convert scientific integer to normal integer
Paul McGuire
ptmcg at austin.rr._bogus_.com
Tue Oct 5 12:20:12 EDT 2004
"les ander" <les_ander at yahoo.com> wrote in message
news:a2972632.0410050751.6b0b39d at posting.google.com...
> Hi,
> i have a file with lines like this:
> 1.7000000e+01 2.4000000e+01 1.0000000e+00 8.0000000e+00
1.5000000e+01
> 2.3000000e+01 5.0000000e+00 7.0000000e+00 1.4000000e+01
1.6000000e+01
> 4.0000000e+00 6.0000000e+00 1.3000000e+01 2.0000000e+01
2.2000000e+01
> 1.0000000e+01 1.2000000e+01 1.9000000e+01 2.1000000e+01
3.0000000e+00
> 1.1000000e+01 1.8000000e+01 2.5000000e+01 2.0000000e+00
9.0000000e+00
>
> Notice that they are all integers.
> What I want to do is write them out in a regular way, by which I mean that
the
> output should look like this:
> 17 24 1 9 15
> 23 5 7 14 16
> etc
>
> I tried the following but it did not work:
> fp=open(argv[1])
> for x in fp:
> xc=[int(e) for e in x.split()]
> print " ".join(xc)
>
>
> any help would be much appreciated
int(e) fails because it doesn't like the decimal point. See below, using
int(float(e)).
-- Paul
---------------------------------
testdata = """
1.7000000e+01 2.4000000e+01 1.0000000e+00 8.0000000e+00
1.5000000e+01
2.3000000e+01 5.0000000e+00 7.0000000e+00 1.4000000e+01
1.6000000e+01
4.0000000e+00 6.0000000e+00 1.3000000e+01 2.0000000e+01
2.2000000e+01
1.0000000e+01 1.2000000e+01 1.9000000e+01 2.1000000e+01
3.0000000e+00
1.1000000e+01 1.8000000e+01 2.5000000e+01 2.0000000e+00
9.0000000e+00
"""
for line in testdata.split('\n'):
print [ int(float(e)) for e in line.split() ]
---------------------------------
gives:
[]
[17, 24, 1, 8, 15]
[23, 5, 7, 14, 16]
[4, 6, 13, 20, 22]
[10, 12, 19, 21, 3]
[11, 18, 25, 2, 9]
[]
More information about the Python-list
mailing list