以正常的python方式......
你不是在迭代这些线......
~ $ cat test.txt
1.0
2.0
3.0
4.5
5.0
6.0
但是,您可以阅读所有行,然后对其应用浮点数:
>>> with open('test.txt', 'r') as f:
... lines = f.readlines()
... foo1=map(float, lines)
... print foo1
...
[1.0, 2.0, 3.0, 4.5, 5.0, 6.0]
>>> sum(foo1)
21.5
但是,您应该使用 NumPy!
总结所有文件的粗略解决方案
import numpy as np
totalsum=0
ListofFiles = ['foo1','foo2']
# from the help of np.loadtxt
# Note that `If the filename extension is .gz or .bz2, the file is first decompressed`
# see the help for that function.
for FileName in ListofFiles:
totalsum=totalsum+np.sum(np.loadtxt(FileName,skiprows=6))
对来自不同文件的元素求和的解决方案
# use with caution it might hog your memory
import numpy as np
totalsum=0
ListofFiles = ['foo1','foo2']
arrayHolder = np.loadtxt(FileName,skiprows=6)
for idx,FileName in enumerate(ListofFiles[1:]):
arrayHolder=np.hstack((arrayHolder,np.loadtxt(FileName,skiprows=6)))
# see documentation for numpy.hstack and my example below.
# now you have a huge numpy array. you can do many things on it
# e.g
# sum each file if the above test.txt had an identical file named test1.txt
np.sum(arrayHolder , axis=0)
# output would be:
array([2.0, 4.0, 6.0, 9.0, 10.0, 12.0])
# sum each ith element accross files
np.sum(arrayHolder , axis=1)
# more extended
In [2]: a=np.array([1.0,2.0,3.0,4.5,5.0,6.0])
In [4]: b=np.array([1.0,2.0,3.0,4.5,5.0,6.0])
In [9]: c=np.vstack((a,b))
In [10]: c
Out[10]:
array([[ 1. , 2. , 3. , 4.5, 5. , 6. ],
[ 1. , 2. , 3. , 4.5, 5. , 6. ]])
In [11]: np.sum(c, axis=0)
Out[11]: array([ 2., 4., 6., 9., 10., 12.])
In [12]: np.sum(c, axis=1)
Out[12]: array([ 21.5, 21.5])
# as I said above this could chocke your memory, so do it gradualy,
# dont try on all 4000 files at once !
请注意,对于 Pierre 提供的解决方案,该解决方案将运行得更快,因为许多 NumPy 函数是编写的,并且是 C 和优化的。如果您需要在 4000 行上运行,我希望 for 循环会更慢...