I have two files:
data1.txt
First Second
1 2
3 4
5 6
...
data2.txt
First Second
6 4
3 9
4 1
...
I would like to add every number from the first file to the number from the second file. And save the output to third file.
So that the outcome would be:
sum.txt
Sum
7 6
6 13
9 7
....
So far I have this code (not working)
with open('data1.txt') as f1, open('data2.txt') as f2, open('sum.txt', 'w') as f_out:
f_out.write(f'Sum1 Sum2\n')
header = next(f1)
c1, c2 = header.strip().split(' ')
header = next(f2)
c1, c2 = header.strip().split(' ')
for line in f1:
line = line.strip()
num1, num2 = line.split(' ')
num1, num2 = int(num1), int(num2)
for line in f2:
line = line.strip()
num1, num2 = line.split(' ')
num1, num2 = int(num1), int(num2)
sum1 = f1(num1) + f2(num1)
sum2 = f1(num2) + f2(num2)
f_out.write(f'{sum1} {sum2}\n')