Krischu
Krischu

Reputation: 1125

numpy genfromtxt strings int mixed data types

I'm trying to read a csv file with the following content into a numpy array:

1,85,104,2,"C"
2,71,82,2,"C#"
3,67,73,2,"D"
4,105,108,2,"D#"
5,103,100,2,"E"

This is the attempt of coding:

import numpy as np
twg=np.genfromtxt(r'./Documents/gears.txt', delimiter=',',dtype=(int,int,int,int,object))
print (twg)

But this eats up the #-sign in the source:

[( 1,  85, 104,   2, b'"C"') ( 2,  71,  82,   2, b'"C')
 ( 3,  67,  73,   2, b'"D"') ( 4, 105, 108,   2, b'"D')
 ( 5, 103, 100,   2, b'"E"')]

Upvotes: 1

Views: 1931

Answers (1)

hpaulj
hpaulj

Reputation: 231325

The # is treated as a comment flag. Quoting doesn't make a difference:

In [345]: txt=b'''1,85,104,2,"C"
     ...: 2,71,82,2,"C#"
     ...: 3,67,73,2,"D"
     ...: 4,105,108,2,"D#"
     ...: 5,103,100,2,"E"'''
In [346]: 
In [346]: np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None)
Out[346]: 
array([(1,  85, 104, 2, b'"C"'), (2,  71,  82, 2, b'"C'),
       (3,  67,  73, 2, b'"D"'), (4, 105, 108, 2, b'"D'),
       (5, 103, 100, 2, b'"E"')],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', 'S3')])

Turning off comments:

In [347]: np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None, comments=None
     ...: )
Out[347]: 
array([(1,  85, 104, 2, b'"C"'), (2,  71,  82, 2, b'"C#"'),
       (3,  67,  73, 2, b'"D"'), (4, 105, 108, 2, b'"D#"'),
       (5, 103, 100, 2, b'"E"')],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', 'S4')])

Upvotes: 2

Related Questions