Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert scientific integer to normal integer

Reply
Thread Tools

convert scientific integer to normal integer

 
 
les ander
Guest
Posts: n/a
 
      10-05-2004
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
 
Reply With Quote
 
 
 
 
wes weston
Guest
Posts: n/a
 
      10-05-2004
les ander wrote:
> 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


Les,
>>> x=eval('1.7000000e+01')
>>> x

17.0
>>>

wes

 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      10-05-2004
"wes weston" <(E-Mail Removed)> wrote in message
news:yDz8d.667038$(E-Mail Removed)...
> les ander wrote:
> > 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

>
> Les,
> >>> x=eval('1.7000000e+01')
> >>> x

> 17.0
> >>>

> wes


Erm, yes, but there is almost always a better way than eval().

Note the following:

>>> float('1.700000e+01')

17.0
>>> int('1.700000e+01')


Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
int('1.700000e+01')
ValueError: invalid literal for int(): 1.700000e+01
>>> int(float('1.70000e+01'))

17

This should be enough to allow you to parse your values, assuming you are
really sure that they are always going to be integers.


--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.


 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      10-05-2004
"les ander" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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]
[]


 
Reply With Quote
 
wes weston
Guest
Posts: n/a
 
      10-05-2004
Russell,
Erm, yes; much better.
wes

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert numpy.ndarray into "normal" array Johannes Bauer Python 4 04-24-2009 11:30 PM
Convert Sent folder to a normal folder Raymond Firefox 3 09-12-2008 09:47 PM
Convert a 'normal' website to an ajax website. Mufasa ASP .Net 4 11-02-2007 04:39 PM
.NET-ey way to convert XML-encoded/escaped entities into normal characters/HTML? ASP .Net 2 06-20-2007 05:32 PM
convert a zip string to a normal string hftmit@gmail.com Java 3 08-11-2006 07:15 AM



Advertisments