【发布时间】:2011-06-20 19:55:36
【问题描述】:
我正在尝试使用带有 converters 参数的 numpy.loadtxt 从文本文件中读取数据。我混合了整数列和字符串。代码是:
a, b, c, d, e = np.loadtxt(infile, delimiter = ',', usecols=(0, 2, 5, 8, 9), skiprows = 1,
unpack = True, converters = dict(zip((0, 2, 5, 8, 9), (int, float, float, int, int))))
数据被正确读入并正确解包,但所有变量(a、b、c、d 和 e)最终都是浮点数。我在转换器语法中犯了错误吗?
编辑尝试回答
我尝试按照@joris 的建议使用 dtype = (int,float,float,int,int):
a,b,c,d,e = np.loadtxt(infile,delimiter = ',', usecols=(0,2,5,8,9), skiprows = 1, unpack = True, dtype = (int,float,float,int,int))
但我收到以下错误:
41 skiprows = 1,
42 unpack = True,
---> 43 dtype = (int,float,float,int,int))
44
45
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack)
665 try:
666 # Make sure we're dealing with a proper dtype
--> 667 dtype = np.dtype(dtype)
668 defconv = _getconv(dtype)
669
TypeError: data type not understood
WARNING: Failure executing file: <forward_NDMMF.py>
我正在使用 NumPy v. 1.5.1。
【问题讨论】: