String Fomat Conversion
Stephen Thorne
stephen.thorne at gmail.com
Thu Jan 27 00:01:52 EST 2005
On 26 Jan 2005 20:53:02 -0800, mcg <mgarrett at garrett-technologies.com> wrote:
> Investigating python day 1:
>
> Data in file:
> x y
> 1 2
> 3 4
> 5 6
>
> Want to read file into an array of pairs.
>
> in c: scanf("%d %d",&x,&y)---store x y in array, loop.
>
> How do I do this in python??
> In the actual application, the pairs are floating pt i.e. -1.003
f = file('input', 'r')
labels = f.readline() # consume the first line of the file.
Easy Option:
for line in f.readlines():
x, y = line.split()
x = float(x)
y = float(y)
Or, more concisely:
for line in f.readlines():
x, y = map(float, line.split())
Regards,
Stephen Thorne
More information about the Python-list
mailing list