[SciPy-User] How To Use Loadtxt For Floats And Strings?
Vincent Davis
vincent@vincentdavis....
Sat Jun 12 12:57:33 CDT 2010
On Sat, Jun 12, 2010 at 11:31 AM, Pierre GM <pgmdevlist@gmail.com> wrote:
> On Jun 12, 2010, at 12:55 PM, Joseph Smidt wrote:
>> a, b, c = loadtxt('myfile.txt',unpack=True,dtype=(float,float,'S16'))
>>
>> I must be doing something wrong. What is the proper expression for
>> loadtxt? Or must I use something else? Thanks!
You can run what is below to better see what is happening but one
problem is that loadtxt used any white space as a delimiter. This is a
problem for "\Omega_b h^2" as "h^2" will be in another column or not
imported which may be what you want? Are the columns separated by
tabs? if so that help, you should specify the delimiter. Also I think
you need to specify the data type as I have below because you are
getting a structured array from this.
from StringIO import StringIO
import numpy as np
d = StringIO("""0.227045E-01 0.610229E-03 \Omega_b h^2
0.110213E+00 0.550143E-02 \Omega_{DM} h^2
0.103980E+01 0.263806E-02 \theta
0.893775E-01 0.147515E-01 \tau""")
print np.loadtxt(d, unpack=True, dtype=[('num1', float),('num2',
float),('s1', '|S16')])
#a, b, c = np.loadtxt(d, unpack=True, dtype=[('num1', float),('num2',
float),('s1', '|S16')])
Vincent
>
> The right side outputs 1 structured array with field names 'f0','f1' and 'f2' by default.
> You can access individual columns by
>>>> tmp = loadtxt('myfile.txt',unpack=True,dtype=(float,float,'S16'))
>>>> (a,b,c) = [tmp["f%i" % i] for i in (0,1,2)]
>
> or something like that
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-User
mailing list