I have a small problem here, I\'m trying to sum up entries from multiple files (50), and each of them contain 3 columns. for example, using the first 3 files: file1.txt, fi
http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html load into numpy array and then sum arrays.
#!/usr/bin/python
b_list = []
for l in range(1,4):
file=open('/Users/sgeorge/file%i.txt' % l, 'r')
m1=[(i.strip()) for i in file]
for j in m1:
b_list.append(filter(None,[(k.strip()) for k in j]))
#print b_list
a = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(0,9,3):
a[0][0] += int(b_list[i][0])
a[0][1] += int(b_list[i][1])
a[0][2] += int(b_list[i][2])
for i in range(1,9,3):
a[1][0] += int(b_list[i][0])
a[1][1] += int(b_list[i][1])
a[1][2] += int(b_list[i][2])
for i in range(2,9,3):
a[2][0] += int(b_list[i][0])
a[2][1] += int(b_list[i][1])
a[2][2] += int(b_list[i][2])
for j in a:
print j[0],j[1],j[2]
Output:
$ python stack.py
9 6 6
4 11 8
12 11 13
The above script is meant to handle only three files (file1.txt
,file2.txt
and file3.txt
)
If you want to handle 50 such files, use following:
#!/usr/bin/python
b_list = []
for l in range(1,51):
file=open('/Users/sgeorge/file%i.txt' % l, 'r')
m1=[(i.strip()) for i in file]
for j in m1:
b_list.append(filter(None,[(k.strip()) for k in j]))
#print b_list
a = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(0,150,3):
a[0][0] += int(b_list[i][0])
a[0][1] += int(b_list[i][1])
a[0][2] += int(b_list[i][2])
for i in range(1,150,3):
a[1][0] += int(b_list[i][0])
a[1][1] += int(b_list[i][1])
a[1][2] += int(b_list[i][2])
for i in range(2,150,3):
a[2][0] += int(b_list[i][0])
a[2][1] += int(b_list[i][1])
a[2][2] += int(b_list[i][2])
for j in a:
print j[0],j[1],j[2]
Update:
>>> a=1
>>> b=1.1
>>> type(a)
<type 'int'>
>>> type(b)
<type 'float'>
>>> float(a)+float(b)
2.1
>>>
My above script will not handle float
ing numbers. For handling the same, use the following:
#!/usr/bin/python
b_list = []
for l in range(1,4):
file=open('/Users/sgeorge/file%i.txt' % l, 'r')
m1=[(i.strip()) for i in file]
for j in m1:
b_list.append(j.replace('\n','').split(' '))
#print b_list
a = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(0,9,3):
a[0][0] += float(b_list[i][0])
a[0][1] += float(b_list[i][1])
a[0][2] += float(b_list[i][2])
for i in range(1,9,3):
a[1][0] += float(b_list[i][0])
a[1][1] += float(b_list[i][1])
a[1][2] += float(b_list[i][2])
for i in range(2,9,3):
a[2][0] += float(b_list[i][0])
a[2][1] += float(b_list[i][1])
a[2][2] += float(b_list[i][2])
for j in a:
print j[0],j[1],j[2]
Output:
$ python stack.py
9.0 6.0 6.0
4.0 11.0 8.0
12.0 11.0 13.0
In [1]: import numpy as np
In [2]: from StringIO import StringIO
In [3]: txt ="""2 3 4
...: 1 5 6
...: 5 4 7"""
In [4]: f = StringIO(txt)
In [5]: arr = np.loadtxt(f,dtype = int)
In [6]: np.sum(arr,axis = 0)
Out[6]: array([ 8, 12, 17])