Newbie question
Stargaming
stargaming at gmail.com
Mon Mar 5 12:03:42 EST 2007
Tommy Grav schrieb:
> Hi list,
>
> this is somewhat of a newbie question that has irritated me for a
> while.
> I have a file test.txt:
>
> 0.3434 0.5322 0.3345
> 1.3435 2.3345 5.3433
>
> and this script
> lines = open("test.txt","r").readlines()
> for line in lines:
> (xin,yin,zin) = line.split()
> x = float(xin)
> y = float(yin)
> z = float(zin)
>
> Is there a way to go from line.split() to x,y,z as floats without
> converting
> each variable individually?
>
> Cheers
> Tommy
For this case, there are list comprehensions (or map, but you shouldn't
use it any longer):
>>> a = "0.3434 0.5322 0.3345"
>>> b = a.split()
>>> map(float, b)
[0.34339999999999998, 0.53220000000000001, 0.33450000000000002]
>>> [float(x) for x in b]
[0.34339999999999998, 0.53220000000000001, 0.33450000000000002]
I think it should be easy to apply this to your example above.
Stargaming
More information about the Python-list
mailing list