Why is my Python "if" condition not working

Question:

I am writing a little utility that will read data from a pipe delimited file. If the 4th field in each row of the file is not empty, I want it write that row to another output file.

I am attempting to loop through each row in the input file, separate out the fields into a list, check to see if the 4th field (3rd index) value is NOT an empty string, and if so, write that row to another file.

Instead, it is writing every row of the original input file to the desired output file, even IF the 4th field (3rd index) value IS an empty string.

What am I doing wrong? Here is my code:

lines = src_file.readlines()

for i in range(len(lines)):    
    fields = lines[i].strip("n").split("|")
   
    if fields[3] != '':
        lines[i] = "|".join(fields) + "n"
    
 
src_file.close()
output_file = open(output_file_name, "w+")
output_file.writelines(lines)
output_file.close()
Asked By: user3377477

||

Answers:

You’re never removing the lines with an empty field from lines.

You don’t need to modify lines[i] (and you’re not, since you’re just joining fields back into the original string that it came from).

You should do the writing to the output file in the loop, so you can skip the lines that don’t meet the condition.

with open(src_file_name) as src_file, open(output_file_name, "w") as output_file:
    for line in src_file:
        fields = line.strip("n").split("|")
        if fields[3]:
            output_file.write(line)
Answered By: Barmar

If I understand correctly, you would like to skip a line if the 3rd index value is an empty string. So the condition should be exactly like "if the 3rd index value is an empty string skip the line".
I’ve edited your code, see if it works:

lines = src_file.readlines()

for i in range(len(lines)):    
    fields = lines[i].strip("n").split("|")
   
    if fields[3] == "":
        lines[i] = ""
    
src_file.close()

lines = [x for x in lines if x != ""]    #add this to skip the empty lines
output_file = open(output_file_name, "w+")
output_file.writelines(lines)
output_file.close()

Answered By: danPho

The issue with your code is that you are writing the whole lines into the new file; that’s why the variable lines contain all the lines.

Here is working code:

output_file = open(output_file_name, "w+")
lines = src_file.readlines()

for i in range(len(lines)):    
    fields = lines[i].strip("n").split("|")

    if fields[3] != '':
        output_file.write("|".join(fields) + "n")

src_file.close()
output_file.close()
Answered By: Stg