user5799809
user5799809

Reputation: 11

Reading files into array python

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.

Upvotes: 1

Views: 60

Answers (2)

Anton Protopopov
Anton Protopopov

Reputation: 31662

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

Upvotes: 2

TigerhawkT3
TigerhawkT3

Reputation: 49320

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

Upvotes: 0

Related Questions