Reading files into array python -


i have multiple files thousands of rows need compare. example want 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

4 10 5   4 10 6   4 10 7  

i wonder best way type of calculations. trying python, having hard time read file python make proper kind of array calculation. thanks.

you use numpy.genfromtxt:

import numpy 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 save array numpy.savetxt format %d if need output integers:

np.savetxt('file3', a3, fmt='%d') 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -