[Numpy-discussion] numpy.genfromtxt converters issue

Lluís xscript at gmx.net
Sat Nov 6 12:26:23 EDT 2010


Sorry, I got it wrong and ignored the StringIO part.

Lluis


Lluís  writes:

> Damien Moore writes:
[...]
>> import numpy, StringIO

>> s=StringIO.StringIO('q1,2\nq3,4')
>> a=numpy.genfromtxt(s,delimiter=',',converters={0:lambda s:float(s[1:])})
>> s=StringIO.StringIO('q1,2\nq3,4')
>> b=numpy.genfromtxt(s,delimiter=',')
>> a.shape
>> (2,)
>> b.shape
>> (2,2)
>>>>> a
>> array([(1.0, 2.0), (3.0, 4.0)], 
>>       dtype=[('f0', '|O4'), ('f1', '<f8')])
>>>>> b
>> array([[ NaN,   2.],
>>        [ NaN,   4.]])


> I think this is what you want:

>>>> cat /tmp/test.csv
> 1, 2
> 3, 4
>>>> numpy.genfromtxt("/tmp/test.csv", delimiter=",", dtype=float)
> array([[ 1.,  2.],
>        [ 3.,  4.]])

> Lluis

-- 
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth



More information about the NumPy-Discussion mailing list