[SciPy-User] Read file with comma decimal separator

Marcel Blattner blattnem@gmail....
Wed Jul 31 08:37:38 CDT 2013


.... Use the  'delimiter' key in the genfromtxt routine. Like in the docu 

>>> s = StringIO("1,1.3,abcde")
>>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
... ('mystring','S5')], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])

Best 
Marcel



On 31.07.2013, at 15:31, Florian Lindner <mailinglists@xgm.de> wrote:

> Am Mittwoch, 31. Juli 2013, 15:25:56 schrieb Florian Lindner:
>> Hello,
>> 
>> I have a file that used comma as a decimal separator. How can I read a file
>> like that using loadtxt or genfromtxt ?
> 
> Since I'm not sure how many columns the file will have, I tried:
> 
>    conv = {}
>    for i in range(1000):
>        conv[i] = lambda a: a.replace(",", ".")
> 
>    data = np.loadtxt(f,  skiprows = 2, converters = conv)
> 
> but:
> 
>  File "/usr/lib/python2.7/site-packages/numpy/lib/npyio.py", line 817, in 
> loadtxt
>    converters[i] = conv
> IndexError: list assignment index out of range
> 
> Regards,
> Florian
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20130731/5946f7ef/attachment.html 


More information about the SciPy-User mailing list