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])))