当打印“奇怪的和”消息时

2024-09-24 22:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python NumPy计算矩阵列的和:

import numpy
from StringIO import StringIO

fileName = 'test2.csv'
myFile = open(fileName,'r')
print "Reading data from '%s' ..." % fileName
data = myFile.read() 
myFile.close()

data = numpy.genfromtxt(StringIO(data), delimiter=',', usecols=(0,1,2))
print "Calculating ..."

print data[:,2]
sumA1 = data[:,2].sum
print "shape =", data.shape
print "sumA1 =", str(sumA1)
print "ok"

test2.csv的内容:

^{pr2}$

我得到这个输出

Reading data from 'test2.csv' ...
Calculating ...
[ 14.   3.  14.   3.  14.   3.  14.   3.  14.]
shape = (9, 3)
sumA1 = <built-in method sum of numpy.ndarray object at 0x00FD8EF8>
ok

为什么我不能用“sumA1=82”代替那个奇怪的信息?我做错什么了?在

事先非常感谢。在


Tags: csvfromimportnumpydatafilenamemyfilesum