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 notebook for 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:ProgramDataAnaconda3libsite-packagesnumpylibnpyio.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 基础知识。
genfromtxt
文档说:
名称 : {无, 真, str, 序列}
您可以将其设置为:
In [689]: str(False)
Out[689]: 'False'
换句话说,您已经告诉它将一个字段命名为"False"。 因此,字段数和列数不匹配。
我怀疑你想要names=None
,意思是,不要从文件中获取字段名称。