Search code examples
pythonif-statementmultiplication

'if statement' removes all lines that don't match the condition, I dont want that


I'm new to Python and I'm struggling with something fairly basic.

What I'm trying to do:

I have a text file consisting of multiple lines. I want the index number [-3] to be multiplied by 2.

However, I only want Python to multiply the index [-3] of every line if index number [6] is equal to 1.

If index number [6] of my line is 0, I don't want to multiply the number by 2. I just want the line to remain in the text file, completely unchanged.

Here's the problem:

If I use the 'if' statement, it perfectly multiplies the index number [-3] if my in line. Example of a multiplied line where the 6th index is 1, so Python is allowed to multiply by 2:

FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.1 0 flbo || This becomes -->

FLBO id 'Boundary' st 0 ty 1 q_ dw 0 0.2 0 flbo

However, if Python sees index number [6] isn't 1, it just skips the line. This is not what I want, because in this way I lose my data. Before I start my script, my text file has 10 lines. The resulting file has 9 lines. So it's lost a line. Example":

FLBO id 'Boundary' st 0 ty 0 q_ dw 0 8 0 flbo || This becomes -->

nothing. line is gone.

My question:

How can I get Python to 'remember' the lines for which the if statement is false?

I have tried to use the pass function, but the result is exactly the same.

My current script / solution:

I have solved the problem in a very ugly way, by multiplying the line where index number [6] = 0 by *1. In this way the number doesnt change. But that's not a clean way of programming, I would like to be more efficient. The code:

all_data = []
with open('C:\\directory1', 'r') as file_handler:
    for line in file_handler.readlines():
        if line.strip():
            each_line_data = line.split()
            if each_line_data[6] == '1':
                old_debiet = each_line_data[-3]
                new_debiet = float(old_debiet) * 2
                each_line_data[-3] = str(new_debiet)
                new_each_line_data = ' '.join(each_line_data)
                all_data.append(new_each_line_data)
            if each_line_data[6] == '0':
                old_debiet = each_line_data[-3]
                new_debiet = float(old_debiet) * 1
                each_line_data[-3] = str(new_debiet)
                new_each_line_data = ' '.join(each_line_data)
                all_data.append(new_each_line_data)
                print(new_each_line_data)

with open('C:\\directory2', 'w') as file_handler:
    for item in all_data:
        file_handler.write("{}\n".format(item))

Solution in the comments. It was an indent problem. Code that works:

all_data = []
with open('C:\\file1', 'r') as file_handler:
    for line in file_handler.readlines():
        if line.strip():
            each_line_data = line.split()
            if each_line_data[6] == '1':
                old_debiet = each_line_data[-3]
                new_debiet = float(old_debiet) * 2
                each_line_data[-3] = str(new_debiet)
            new_each_line_data = ' '.join(each_line_data)
            all_data.append(new_each_line_data)
            print(new_each_line_data)

with open('C:\\file2', 'w') as file_handler:
    for item in all_data:
        file_handler.write("{}\n".format(item))


Solution

  • You need to see what each line does and un-indent whatever needs to be done to every line:

    all_data = []
    with open('C:\\directory1', 'r') as file_handler:
        for line in file_handler.readlines():
            if line.strip():
                each_line_data = line.split()
                if each_line_data[6] == '1':
                    old_debiet = each_line_data[-3]
                    new_debiet = float(old_debiet) * 2
                    each_line_data[-3] = str(new_debiet)
                new_each_line_data = ' '.join(each_line_data)
                all_data.append(new_each_line_data)
                print(new_each_line_data)
    
    with open('C:\\directory2', 'w') as file_handler:
        for item in all_data:
            file_handler.write("{}\n".format(item))
    

    As you can see, joining the line back and appending now happens outside of the =='1' if. This is exactly what you need to do for every line, no matter whether it has 1 or 0, or anything else. :)