[SciPy-User] Read file with comma decimal separator

gary ruben gary.ruben@gmail....
Wed Jul 31 09:27:16 CDT 2013


You could preread the input using StringIO

import StringIO
import numpy as np

s = open('test.txt').read().replace(',','.')
data = np.loadtxt(StringIO.StringIO(s))
print data


On 31 July 2013 23:40, Pierre Gerard-Marchant <pgmdevlist@gmail.com> wrote:

>
> On Jul 31, 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 ?
>
> A quick and dirty approach would be to create a generator that would parse
> your initial input and replace the ',' by '.' on each line. You'd just have
> to feed the generator to `genfromtxt`:
>
> >>>X = StringIO('1,1\t1,2\t1,3\n2,1\t2,2\t,2,3')
> >>>replaced = (line.replace(",", ".") for line in X)
> >>>np.genfromtxt(replaced, delimiter="\t")
>
> Of course, that'd work only if you don't intend to use "," as your
> delimiter, in which case you're out of luck.
> _______________________________________________
> 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/20130801/610894e6/attachment.html 


More information about the SciPy-User mailing list