简体   繁体   中英

Python3 - calculate from multiple files and save to new file

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

You need to iterate on both files simultaneously. If you iterate on the first file, then on the second file, then you are not seeing the numbers from file1 at the same time as the corresponding numbers from file2, so you cannot add them.

with open('data1.txt','r') as f1, open('data2.txt','r') as f2, open('sum.txt', 'w') as f_out:
    h1, h2 = next(f1), next(f2)
    f_out.write(f'Sum1 Sum2\n')
    for line1, line2 in zip(f1, f2):
        a1, b1 = line1.strip().split()
        a2, b2 = line2.strip().split()
        f_out.write('{} {}\n'.format(int(a1)+int(a2), int(b1)+int(b2)))

Note that this might or might not behave the way you expect it to if the two input files have different number of lines. One way to fix the behaviour to better suit your needs would be to use a while loop and call next(f1) and next(f2) manually inside of the loop, catching exceptions with two try/except blocks. Another way would be to use some variant of zip: see for instance Is there a zip-like function that pads to longest length?

Instead of using .strip().split() on every line, you could open the files with the csv module and get lists directly. Documentation for the csv module: https://docs.python.org/3/library/csv.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM