[SciPy-User] Ignore characters while reading text
Florian Lindner
mailinglists at xgm.de
Mon Jun 17 16:46:04 EDT 2013
Am Freitag, 14. Juni 2013, 07:32:09 schrieb Matt Newville:
> On Fri, Jun 14, 2013 at 5:50 AM, Florian Lindner <mailinglists at xgm.de>
wrote:
> > Hello,
> >
> > I have a text file with data like
> >
> > 1 (2 3 4) (5 6 7) (8 9 10)
> > 2 (4 5 1) (3 6 8) (1 6 45)
> >
> > How can I read that file into an array?
> >
> > [
> > [1, 2, 3, 5, 6, 7, 8, 9, 10]
> > [2, 4, 1, 3, ... ]
> > ]
> >
> > I tried genfromtxt with deletechars="()" but that seems to affect only
> > 'names'. I also tried delimiter="() " but that didn't work either.
>
> Would this do?
>
> import numpy as np
> from cStringIO import StringIO
> txt= '1 (2 3 4) (5 6 7) (8 9 10)'
> np.loadtxt(StringIO(txt.replace('(', '').replace(')', '')))
Thanks!
Speed is not an issue here, it's a just a (8000, 20) array.
More information about the SciPy-User
mailing list