[Numpy-discussion] numpy input with genfromttxt()

Bruce Southey bsouthey at gmail.com
Fri Jun 3 11:01:09 EDT 2011


On 06/03/2011 09:33 AM, jonasr wrote:
> thank you very much, works much nicer and faster in comparison to the script
> i wrote and used before  ,
> im not that much used to lambda forms but it seems quit usefull in
> situations like this
>
>
> Olivier Delalleau-2 wrote:
>> Here's an ugly one-liner:
>>
>> numpy.genfromtxt('data.txt', converters=dict([k, lambda x:
>> float(x.replace(',', '.'))] for k in
>> range(len(open('data.txt').readline().strip().split()))))
>>
>> -=- Olivier
>>
>> 2011/6/3 jgrub<jonas.ruebsam at web.de>
>>
>>> Hello, im actually try to read in  data with genfromtxt(),
>>> i want to read in numbers which are stored in  a textfile like this:
>>>
>>> 0,000000        0,001221        0,001221        0,000000        1,278076
>>>       160,102539
>>>
>>> 4,000000E-7     0,000000        0,000000        0,002441        1,279297
>>>       160,000000
>>>
>>> 8,000000E-7     -0,001221       0,000000        0,001221        1,279297
>>>       159,897461
>>>
>>> 1,200000E-6     0,000000        0,000000        0,001221        1,279297
>>>       160,000000
>>>
>>> 1,600000E-6     -0,001221       0,000000        0,003662        1,278076
>>>       159,897461
>>>
>>> 2,000000E-6     0,000000        -0,001221       0,003662        1,279297
>>>       160,000000
>>>
>>> my problem is that they are seperated with a comma so when i try to read
>>> them
>>> i just get a numpy array with NaN's.  is there a short way to replace the
>>> "," with "."  ?
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/numpy-input-with-genfromttxt%28%29-tp31757790p31757790.html
>>> Sent from the Numpy-discussion mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> NumPy-Discussion at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
Isn't this just because of the 'locale' settings?
A quick search showed  ticket 884 that has code changing the locale that 
may be useful for you:
http://projects.scipy.org/numpy/ticket/884

Perhaps a similar bug exists with genfromtxt?

If it is nicely behaved, just use Python's csv module.
 From the csv documentation:
"Since open() 
<http://docs.python.org/release/3.1.3/library/functions.html#open> is 
used to open a CSV file for reading, the file will by default be decoded 
into unicode using the system default encoding (see 
locale.getpreferredencoding() 
<http://docs.python.org/release/3.1.3/library/locale.html#locale.getpreferredencoding>). 
To decode a file using a different encoding, use the encoding argument 
of open:"


Bruce


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110603/ad0a686b/attachment.html>


More information about the NumPy-Discussion mailing list