I'm programming in Python and reading from a CSV template with Numpy like this:
csv = np.genfromtxt('MMRExport.csv',delimiter=",", dtype=str)
But inside the CSV I have rows of empty data and it doesn't take them into account, for example the first row has:
"A","B","C","","D","E",""
Then the columns that have empty fields are not considered or it throws me an error in the cases that following rows have data in that location. For example, if row 2 is the same shape as row 1 there is no problem, but if another row was of the form:
"1","","2","3","4","5","6"
It throws me an error since the number of columns are different. What I want is to read all the columns even the empty ones. How could I do that?
The error it throws me is this:
Traceback (most recent call last):
File "/Users/JPontigo/Downloads/Tarea.py", line 17, in <module>
csv = np.genfromtxt('MMRExport.csv',delimiter=",", dtype=str)
File "/Users/JPontigo/anaconda/lib/python3.5/site- packages/numpy/lib/npyio.py", line 1769, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #2 (got 27 columns instead of 5)
Line #3 (got 27 columns instead of 5)
Line #4 (got 27 columns instead of 5)
Line #5 (got 27 columns instead of 5)
Line #6 (got 2 columns instead of 5)
Line #7 (got 27 columns instead of 5)
Line #8 (got 27 columns instead of 5)
Line #9 (got 27 columns instead of 5)
Line #10 (got 27 columns instead of 5)
The file is this:
https://drive.google.com/file/d/0B11sJdX_AaJBWGNabERCMzhiYVk/view?usp=sharing
np.genfromtxt
has a parametercomments
to allow the use of comments in the txt. This parameter receives a character that indicates that the text after it is a comment, so it is eliminated when creating the array. By default the value ofcomments
is'#'
. The first row of your csv is:Since it interprets that everything after
#
is a comment, it only takes into account:That is, 5 columns. The rest of the rows have 27 columns (there are no characters
#
and they are read in full) which causes the error.The solution is simple, change the parameter
comments
: