ericksonla
ericksonla

Reputation: 1363

numpy genfromtxt breaks on #

I have a csv file that includes some #:

a,1,asdf
a#,2,asdf

When I try to use numpy genfromtxt, it fails:

data = np.genfromtxt('TestCSV.csv', delimiter=',')

ValueError: Some errors were detected !
Line #2 (got 1 columns instead of 3)

As far as I can tell, the # is the problem. What is going on here? Is there a way to fix it?

Upvotes: 1

Views: 249

Answers (1)

gabra
gabra

Reputation: 10544

The # is a special character for comments. To load your data with the genfromtxt, you have to replace it.

numpy.genfromtxt('txt', delimiter=',', dtype=str, comments='%')

And the output is:

array([['a', '1', 'asdf'],
       ['a#', '2', 'asdf']], 
      dtype='|S4')

Note that I replaced it by %.

Also, since your data has several types, I had to define dtype.

Upvotes: 4

Related Questions