0

I have two input files (input1.txt and input2.txt). I would multiply the first column of the first input file (input1.txt) to the third column of the second input file (input2.txt) and sum the second columns of both input files and write the results to the output file.

I tried the below code but it gives the error. How can I fix my code?

Error:

SyntaxError: unexpected EOF while parsing

input1.txt:

2.5 1.2
5.5 6.5
3.2 6.3

input2.txt:

10.5 12.5 20.2
13.1 14.5 30.1
15.9 16.7 40.2

Desired output.txt:

#first data second data
50.20   13.7
165.55  21.0
128.64  23.0

Code:

#!/usr/bin/env python3

with open('output.txt', mode='w') as f:
    with open("1.txt") as f1:
        data1 = f1.readlines()
        for line1 in data1:
            lines1 = line1.strip('')
            with open("2.txt") as f2:
                data2 = f2.readlines()
                for line2 in data2:
                    lines2 = line2.strip('')
                    f.write ("%.3f %.3f\n" % (float(lines1[0]*float(lines2[2]),
                                              float(lines1[1]+float(line1[1])))
martineau
  • 119,623
  • 25
  • 170
  • 301
qasim
  • 427
  • 2
  • 5
  • 14
  • 1
    Please provide the entire error message. Have you done some debugging, any research? – AMC Apr 14 '20 at 01:15
  • 3
    Does this answer your question? [SyntaxError: unexpected EOF while parsing](https://stackoverflow.com/questions/43189302/syntaxerror-unexpected-eof-while-parsing) – AMC Apr 14 '20 at 01:15
  • Yep, final write doesn't have enough closing parenthesissisus. – Maarten Bodewes Apr 14 '20 at 01:35

1 Answers1

1

After pasting your code into an editor, I can see that your missing some closing parentheses at the end of your f.write() call, which is causing the SyntaxError: unexpected EOF while parsing error. Add those missing ) and the error should go away. You also are forgetting to split the lines into separate elements. This will cause a ValueError when doing a float conversion.

Your code with no errors:

with open('output.txt', mode='w') as f:
    with open("1.txt") as f1:
        data1 = f1.readlines()
        for line1 in data1:
            lines1 = line1.strip('').split()
            with open("2.txt") as f2:
                data2 = f2.readlines()
                for line2 in data2:
                    lines2 = line2.strip('').split()
                    f.write ("%.3f %.3f\n" % (float(lines1[0])*float(lines2[2]),
                                              float(lines1[1])+float(lines1[1])))

However, the logic needs tweaking. The above code is iterating every line from the first input file, and then inside that you are iterating every line from the second file. This will give you a cartesian product of lines, which I don't believe is what you want.

We can see this from the incorrect output it creates:

50.500 2.400
75.250 2.400
100.500 2.400
111.100 13.000
165.550 13.000
221.100 13.000
64.640 12.600
96.320 12.600
128.640 12.600

Instead, you can iterate both files in parallel. You can use zip to aggregate both two input files together to do the calculations between their respective columns:

# Open both input files and output file
with open("input1.txt") as input1, open("input2.txt") as input2, open("output.txt", mode="w") as out:

    # Zip both inputs so we can do column matching
    for line1, line2 in zip(input1, input2):

        # Split lines into lists representing columns
        col1, col2 = line1.split(), line2.split()

        # Do both calculations
        # Round the first one to 2 decimal places and second one to 1 decimal place
        first_calc = round(float(col1[0]) * float(col2[2]), 2)
        second_calc = round(float(col1[1]) + float(col2[1]), 1)

        # Write to file
        out.write(f"{first_calc} {second_calc}\n")

Which gives the following output.txt:

50.5 13.7
165.55 21.0
128.64 23.0

I'll leave the final formatting for you to do :-)

RoadRunner
  • 25,803
  • 6
  • 42
  • 75