1

I have multiple files with thousands of rows that I need to compare. for example I want to do subtraction file3 = file2 - file1,

file1  
1 10 5  
2 20 4  
3 30 3



file2  
5 20 10  
6 30 10  
7 40 10 

file3 would be

4 10 5  
4 10 6  
4 10 7 

I wonder what is the best way to do this type of calculations. I am trying Python, but I am having a hard time to read the file to python to make it the proper kind of array for calculation. Thanks.

2 Answers 2

2

You could use numpy.genfromtxt:

import numpy as np
a1 = np.genfromtxt('file1')
a2 = np.genfromtxt('file2')
a3 = a2 - a1
print(a3)
array([[  4.,  10.,   5.],
       [  4.,  10.,   6.],
       [  4.,  10.,   7.]])

Then you could save that array with numpy.savetxt with format %d if you need output as integers:

np.savetxt('file3', a3, fmt='%d')
Sign up to request clarification or add additional context in comments.

Comments

0

Open both files, then loop through them with zip:

with open('file1.txt') as first, open('file2.txt') as second, open('file3.txt', 'w') as output:
    for a, b in zip(first, second):
        a = map(int, a.split())
        b = map(int, b.split())
        output.write(' '.join(map(str, (y-x for x,y in zip(a,b)))) + '\n')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.