[SciPy-User] Read file with comma decimal separator
Wed Jul 31 09:27:16 CDT 2013
You could preread the input using StringIO
import numpy as np
s = open('test.txt').read().replace(',','.')
data = np.loadtxt(StringIO.StringIO(s))
On 31 July 2013 23:40, Pierre Gerard-Marchant <firstname.lastname@example.org> wrote:
> On Jul 31, 2013, at 15:31 , Florian Lindner <email@example.com> 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
> >> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-User