[SciPy-User] Can loadtxt do this: complex file format

Daπid davidmenhur at gmail.com
Fri Nov 23 20:24:51 EST 2012


Answering your first question: yes, you can use the arg "converters".
I used it when I had to load data where the decimal separator was a
comma for the first column and a dot for the rest of the file (a
professional equipment, BTW).

In your case, it would be something like (not tested):

converters = {2: lambda s: float(s[1:)), 4: lambda s: float(s[:-1)),
5: lambda s: float(s[1:)), 7: lambda s: float(s[:-1)))


I don't think I understand your second question. loadtxt will load the
whole file, regardless of the number of columns or rows (and alocate
memory dynamically).



David.

On Fri, Nov 23, 2012 at 3:33 PM, François Beaubert
<francois.beaubert at univ-valenciennes.fr> wrote:
> Hi alll,
>
> I'm wondering if loadtxt can be used to read a file with a rather
> complicated file format (well at least for me :))
>
> I would like to read data in a file (100 000 lines) which contains the Ux,
> Uy and Uz velocity components versus time for different points (x, y, z
> coordinates).
>
>  The file's format looks like this:
>
> #               x                 0             0.005
> #               y                 0            0.0025
> #               z               0.3               0.3
> #            Time
>               100       (7.665475124e-05 -0.0002514304858 0.07519945952)
> (0.0007792405436 -0.001695097619 0.1563337147)
>               200       (7.060958587e-05 -0.0002343546897 0.07631018852)
> (0.0007458315198 -0.001621335844 0.1705269028)
>               300       (6.511531713e-05 -0.0002186820111 0.07784758393)
> (0.0007208548956 -0.001563533763 0.1811116196)
>               400       (6.019627747e-05 -0.0002045291128 0.07981817652)
> (0.000700274915 -0.001515310657 0.1896792906)
>
> The first column is ... the time
> The 3 values in the first parentheses are the Ux, Uy and Uz velocity
> components on point 1, the values enclosed in the second parentheses are the
> Ux,Uy and Uz velocity components for point 2, etc
>
> 1. Is it possible to skip those parentheses to be able to read the data with
> loadtxt ?
> 2. Is it possible to store the points coordinates (referenced in the
> heading) in variables without a priori knowing the total numbers of
> positions ?
>
> Sorry if it looks like to easy for you, but I"m a newbie in python
> Any help will be welcome.
>
> Thanks a lot !
> Cheers
> Francois
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list