0
我有一个CSV一些数据被格式化为如(I删除为简单起见一些列):Numpy无法正确接受字符串?
Year,Region,Round,Diff
2014,South,Second Round,-24
2015,West,First Round,48
# ...lots of rows of this
我希望同时使用在Region
和Round
列中的字符串数据,并在整数数据Diff
栏。
这里是我的相关代码:
import sklearn
import numpy as np
from numpy import genfromtxt
from StringIO import StringIO
# Some other code...
my_dtype=[('Year', int), ('Region', str),('Round', str),('Diff', int)]
data = np.genfromtxt(my_file, delimiter=',',names=True,dtype=my_dtype)
print data
当我打印我的数据,我得到以下。 NumPy使每个字符串都是一个空字符串。
[ (2014, '', '', -24)
(2010, '', '', 48)
...]
有谁知道我该如何解决这个问题?我使用dtype属性是否错误?或者是其他东西?提前致谢。