[SciPy-User] Ignore characters while reading text

Daπid davidmenhur@gmail....
Fri Jun 14 07:46:22 CDT 2013


On 14 June 2013 14:32, Matt Newville <newville@cars.uchicago.edu> wrote:
> 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(')', '')))

If I am not mistaken, then you are reading the data twice or thrice.
If this is big, and performance is critical, you may be better off
doing the loadtxt yourself. The core of np.loadtxt is essencially a
"from line in file: data.append(parse(line))", with some wrapping
intelligence, that probably is not needed in your case.

https://github.com/numpy/numpy/blob/v1.7.0/numpy/lib/npyio.py#L610  ---->loadtxt
https://github.com/numpy/numpy/blob/v1.7.0/numpy/lib/npyio.py#L1573
--->genfromtxt


David.


More information about the SciPy-User mailing list