简体   繁体   English

使用numpy genfromtxt和loadtxt读取csv文件

[英]Reading csv file with numpy genfromtxt and loadtxt

Here is a sample of the data am trying to read: 以下是尝试读取的数据示例:

107,4205,934,-1869,97,3917,0,-5154,23,-447
69,-1014,-1449,-4347,177,-1977,4463,2824,1,80
177,-1581,5028,4519,161,-372,5900,1677,2,36
175,-800,-5028,-2571,195,-1794,-6410,-3589,1,-25
107,-1962,-3457,-2336,109,-2018,-7614,-1376,1,9
72,-138,-6388,1388,76,1447,789,6315,1,38
78,1538,6666,4871,67,5074,6119,7611,3,-50
65,-4615,307,-9384,85,-6235,-5176,-7999,3,-12

however I keep getting these errors 但是我不断收到这些错误

>>> dataset = numpy.loadtxt(r"C:\data.csv",delimiter=',',dtype=str)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
dataset = numpy.loadtxt(r"C:\data.csv",delimiter=',',dtype=str)
File "C:\Program Files\Python36\lib\site-packages\numpy\lib\npyio.py", line 
1101, in loadtxt
for x in read_data(_loadtxt_chunksize):
 File "C:\Program Files\Python36\lib\site-packages\numpy\lib\npyio.py", line 
1025, in read_data
% line_num)
ValueError: Wrong number of columns at line 2

and

>>> dataset = numpy.genfromtxt(r"C:\data.csv",delimiter=',')
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
dataset = numpy.genfromtxt(r"C:\data.csv",delimiter=',')
File "C:\Program Files\Python36\lib\site-packages\numpy\lib\npyio.py", line 
2031, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #2 (got 1 columns instead of 10)
Line #4 (got 1 columns instead of 10)
Line #6 (got 1 columns instead of 10)

I have tried altering the inputs of both functions loadtxt and genfromtxt but nothin seems to work. 我试图改变两个函数loadtxt和genfromtxt的输入,但是nothin似乎起作用。 Also the error about wrong number of columns at line 2 is curious. 同样,关于第2行的列数错误的错误也令人好奇。 This data is straightforward without a header. 该数据很简单,没有标题。 What am I doing wrong? 我究竟做错了什么?

My csv file had been written in Unicode not ANSI. 我的csv文件是用Unicode而不是ANSI编写的。

On changing to ANSI it worked without problems! 在更改为ANSI时,它没有任何问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM