[SciPy-User] Ignore characters while reading text

Matt Newville newville@cars.uchicago....
Fri Jun 14 07:32:09 CDT 2013


On Fri, Jun 14, 2013 at 5:50 AM, Florian Lindner <mailinglists@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(')', '')))

--Matt


More information about the SciPy-User mailing list