convert scientific integer to normal integer

Discussion in 'Python' started by les ander, Oct 5, 2004.

  1. les ander

    les ander Guest

    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 ander, Oct 5, 2004
    #1
    1. Advertising

  2. les ander

    wes weston Guest

    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
     
    wes weston, Oct 5, 2004
    #2
    1. Advertising

  3. les ander

    Russell Blau Guest

    "wes weston" <> wrote in message
    news:yDz8d.667038$...
    > 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.
     
    Russell Blau, Oct 5, 2004
    #3
  4. les ander

    Paul McGuire Guest

    "les ander" <> wrote in message
    news:...
    > 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]
    []
     
    Paul McGuire, Oct 5, 2004
    #4
  5. les ander

    wes weston Guest

    Russell,
    Erm, yes; much better.
    wes
     
    wes weston, Oct 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    10,745
    Ingo R. Homann
    Aug 11, 2006
  2. Guest
    Replies:
    2
    Views:
    781
    Guest
    Jun 20, 2007
  3. Mufasa
    Replies:
    4
    Views:
    396
    Lloyd Sheen
    Nov 2, 2007
  4. Johannes Bauer

    Convert numpy.ndarray into "normal" array

    Johannes Bauer, Apr 24, 2009, in forum: Python
    Replies:
    4
    Views:
    1,787
    Piet van Oostrum
    Apr 25, 2009
  5. Kioko --
    Replies:
    8
    Views:
    169
    Fabian Streitel
    Sep 8, 2009
Loading...

Share This Page