ValueError:使用genfromtxt时,元组的大小必须与字段数匹配

时间:2017-07-02 17:35:25

标签: python numpy ipython ipython-notebook genfromtxt

import os
import numpy as np
os.getcwd()
os.chdir('C:/Users/Gururaj/Desktop/tech/python/')
data = np.genfromtxt("simple.txt", dtype=None, delimiter=",", 
names=str(False))
print(data[0])

=========================错误如下=================== ===

这是在Jupyter笔记本中为python 3

完成的
  ValueError                                Traceback (most recent call 
  last)
  <ipython-input-34-c2ba85f75012> in <module>()
  3 os.getcwd()
  4 os.chdir('C:/Users/Gururaj/Desktop/tech/python/')
  ----> 5 data = np.genfromtxt("simple.txt", dtype=None, delimiter=",", 
  names=str(False))
  6 print(data[0])

  C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py in 
  genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, 
  converters, missing_values, filling_values, usecols, names, excludelist, 
  deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, 
  usemask, loose, invalid_raise, max_rows)
  1874             ddtype = list(zip(names, column_types))
  1875             mdtype = list(zip(names, [np.bool] * len(column_types)))
  -> 1876         output = np.array(data, dtype=ddtype)
  1877         if usemask:
  1878             outputmask = np.array(masks, dtype=mdtype)

  ValueError: size of tuple must match number of fields.
  ===========================================================
  Data in file:
  this,is,me,learning
  monty,pythons,flying,circus
  sounds,exciting,but,hmm

请帮忙,因为我现在才开始学习Python基础知识。

1 个答案:

答案 0 :(得分:1)

genfromtxt文档说:

  

名称:{None,True,str,sequence}

你把它设置为:

In [689]: str(False)
Out[689]: 'False'

换句话说,你已经告诉它将一个字段命名为'False'。因此,字段数和列数之间不匹配。

我怀疑你想要names=None,这意味着,不要从文件中获取字段名称。