我有一个需要使用np.recfromcsv读取的csv文件。我遇到的问题是其中一个列缺少其名称。您可以在此处查看csv文件:https://github.com/btel/visualization-talk/blob/master/Exercises/data/crabs.csv。第一列缺少其标题名称。当我用下面的代码读取文件时,它给了我错误:
File "prob1.py", line 11, in <module>
main()
File "prob1.py", line 7, in main
data = np.recfromcsv('crabs.csv', delimiter=",", names=True)
File "/Library/Python/2.7/site-packages/numpy-1.9.0-py2.7-macosx-10.9-intel.egg/numpy/lib/npyio.py", line 1904, in recfromcsv
output = genfromtxt(fname, **kwargs)
File "/Library/Python/2.7/site-packages/numpy-1.9.0-py2.7-macosx-10.9-intel.egg/numpy/lib/npyio.py", line 1686, in genfromtxt
raise ValueError(errmsg)
我通过使用参数Names = None修复了错误,这样做它为每列提供了一个来自f0,f1的默认名称......但是我只希望在没有为列提供名称时。
我的代码如下所示:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl
def main ():
data = np.recfromcsv('crabs.csv', names=None)
print data.dtype.fields
if __name__ == '__main__':
main()
答案 0 :(得分:0)
我在主要功能中添加了一行代码和三行注释, 请看看这是否是你需要的......
def main ():
data = np.recfromcsv('crabs.csv', names=None)
# I'm using the column names in your csv file, to which I prepended a dummy name
# you should be able to reopen/rewind the file and read the header line
# to get the correct names in similar cases
data.dtype.names = ('n', 'sp', 'sex', 'index', 'FL', 'RW', 'CL', 'CW', 'BD')
print data.dtype.fields