2017-02-15 28 views
0

我讀一個CSV文件和取值,numpy的

numpy.genfromtxt(csv_name, delimiter=',') 

,但我不能這樣做,因爲我的CSV包含不同沒有對每行的列。

O/P:

ValueError: Some errors were detected 
Line #2 (got 8 columns instead of 7) 
Line #3 (got 8 columns instead of 7) 
Line #4 (got 8 columns instead of 7) 
Line #6 (got 8 columns instead of 7) 
Line #7 (got 5 columns instead of 7) 
Line #8 (got 5 columns instead of 7) 
Line #9 (got 5 columns instead of 7) 
Line #10 (got 5 columns instead of 7) 

是否是可能的numpy的呢?

+0

[導入CSV具有不同的每使用熊貓排列(可能的重複http://stackoverflow.com/questions/27020216/import-csv-with - 不同列數的每行使用熊貓) – elcombato

+0

[可變重複的Python中的genfromtxt()中的列的變量?](http://stackoverflow.com/questions/19152432/variable-number -of-columns-in-genfromtxt-in-python) –

回答

0

如果它是確定限制被讀取到列的所有行共同號數據,你可以使用usecols明確地選擇列來讀取,例如:

numpy.genfromtxt(csv_name, delimiter=',', usecols=range(4)) 
+0

無法正常工作,因爲所有數據都是nan的 –

+0

這可能與CSV文件中的實際數據有關。需要數據樣本來闡明這一點。 – languitar

+0

您可能需要指定'dtype' – hpaulj