1

I am reading a csv with

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

but I am unable to do so because my csv contains different no of columns for each row.

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)

Is is possible to do with numpy?

Rohan Nagalkar
  • 433
  • 2
  • 5
  • 15
  • Possible duplicate of [import csv with different number of columns per row using Pandas](http://stackoverflow.com/questions/27020216/import-csv-with-different-number-of-columns-per-row-using-pandas) – elcombato Feb 15 '17 at 12:50
  • Possible duplicate of [Variable Number of Columns in genfromtxt() in Python?](http://stackoverflow.com/questions/19152432/variable-number-of-columns-in-genfromtxt-in-python) – Juan Antonio Feb 15 '17 at 12:53

2 Answers2

1

https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html, you can do it using the filling_values argument of genfromtxt.

Otherwise, you could use this answer: Python: How to read a data file with uneven number of columns

Community
  • 1
  • 1
Zaccharie Ramzi
  • 2,106
  • 1
  • 18
  • 37
0

If it is ok to restrict the data that is read to the common number of columns across all rows, you can use usecols to explicitly select the columns to read, e.g.:

numpy.genfromtxt(csv_name, delimiter=',', usecols=range(4))
languitar
  • 6,554
  • 2
  • 37
  • 62