无法在Python中读取大型CSV

无法在Python中读取大型CSV,python,csv,Python,Csv,我正在使用Python2.7并尝试读取CSV文件的条目。我制作了一个单独版本的原始CSV,它只包含前10行数据,使用下面的代码,它按照我希望的方式工作,我可以在genfromtxt的usecols字段中编辑Z的索引,以读取CSV中特定范围的列 import numpy as np import array Z = array.array('i', (i for i in range(0, 40))) with open('data/training_edit.csv','r') as f:

我正在使用Python2.7并尝试读取CSV文件的条目。我制作了一个单独版本的原始CSV,它只包含前10行数据,使用下面的代码,它按照我希望的方式工作,我可以在genfromtxt的usecols字段中编辑Z的索引,以读取CSV中特定范围的列

import numpy as np
import array

Z = array.array('i', (i for i in range(0, 40)))

with open('data/training_edit.csv','r') as f:
    data = np.genfromtxt(f, dtype=float, delimiter=',', names=True, usecols=(Z[0:32])) 
print(data)
但当我将这段代码用于原始CSV 250000行x 33列时,我得到了这种输出,我不知道为什么:

Traceback (most recent call last):
File "/home/user/PycharmProjects/H-B2/Read.py", line 74, in <module>
data = np.genfromtxt(f, dtype=float, delimiter=',', names=True,usecols=(Z[0:32]))
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1667, in genfromtxt  
raise ValueError(errmsg)
ValueError: Some errors were detected !

.
.
.   
Line #249991 (got 1 columns instead of 32)
Line #249992 (got 1 columns instead of 32)
Line #249993 (got 1 columns instead of 32)
Line #249994 (got 1 columns instead of 32)
Line #249995 (got 1 columns instead of 32)
Line #249996 (got 1 columns instead of 32)
Line #249997 (got 1 columns instead of 32)
Line #249998 (got 1 columns instead of 32)
Line #249999 (got 1 columns instead of 32)
Line #250000 (got 1 columns instead of 32)

Process finished with exit code 1

我添加点只是为了缩短实际输出,但希望您能得到点

是的,我相信您只需要将范围0,32添加到您的使用中,如下所示:

data=np.genfromtxtf,dtype=float,分隔符=',', names=True,usecols=range0,32


我只是自己想出来的。

这似乎是文件中的一个问题。您检查过了吗?您可以轻松地生成另一个具有相同行数的文件,您知道该行数是有效的,并在该文件上运行相同的代码,以查看它是否是大小或格式问题。这不应该是大小。您的csv格式很可能不正确,例如缺少列等。我的原始csv 250000行x 33列-这是您应该验证的假设。处理异常并输出遇到预期时正在处理的行号。你很可能会很快发现这个问题。