当我试图使用genfromtxt读取一个以空格分隔的文件,并使用转换器函数将逗号作为小数分隔符转换数字时,会出现类型错误。似乎我的转换器函数有问题。但是,当我在单个值上使用它时,它可以正常工作。
这是我的代码(我正在使用Matplotlib/Pylab):
数据看起来像这样:
这是我的代码(我正在使用Matplotlib/Pylab):
t = dtype([('Date', 'U12'), ('Time', 'U10'), ('Cond', 'f4'), ('Temp', 'f4')])
conv = lambda valstr: float(valstr.replace(',','.'))
c = {2:conv, 3:conv}
data = genfromtxt('Example.csv', dtype = t,
skip_header=1, delimiter = ' ', converters = c)
数据看起来像这样:
Date Time Cond Temp
11-10-2012 00:00:14 5,430583 29,5107
11-10-2012 00:00:15 5,431812 29,45066
11-10-2012 00:00:16 5,435501 29,43862
11-10-2012 00:00:17 5,436732 29,43862
...
以下是错误信息的一部分:
TypeError Traceback (most recent call last)
<ipython-input-41-c65c2d17c55d> in <module>()
5 c = {2:conv, 3:conv}
6
----> 7 data = genfromtxt('Example.csv', dtype = t, skip_header=1, delimiter = ' ', converters = c)
...
<ipython-input-41-c65c2d17c55d> in <lambda>(valstr)
1 t = dtype([('Date', 'U12'), ('Time', 'U10'), ('Cond', 'f4'), ('Temp', 'f4')])
2
----> 3 conv = lambda valstr: float(valstr.replace(',','.'))
4
5 c = {2:conv, 3:conv}
TypeError: expected an object with the buffer interface
我在这里做错了什么,还是genfromtxt中存在某种错误?
我正在使用Win7 x64上的Python 3.2版本。Numpy版本为1.6.2。