How to extract 2 integers from a string in python?

Discussion in 'Python' started by yinglcs@gmail.com, Jun 9, 2006.

  1. Guest

    Hi,

    how can I extract 2 integers from a string in python?

    for example, my source string is this:
    Total size: 173233 (371587)

    I want to extract the integer 173233 and 371587 from that soource
    string, how can I do that?
     
    , Jun 9, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > how can I extract 2 integers from a string in python?
    >
    > for example, my source string is this:
    > Total size: 173233 (371587)
    >
    > I want to extract the integer 173233 and 371587 from that soource
    > string, how can I do that?
    >


    Use split() to split the string into four strings, using spaces as
    separators, then use int() to convert the resulting strings that
    interest you.

    >>> a, b, c, d = 'Total size: 173233 (371857)'.split()
    >>> first_int, second_int = int(c), int(d[1:-1])
    >>> first_int

    173233
    >>> second_int

    371857

    HTH
    Steve P
     
    Stephen Prinster, Jun 9, 2006
    #2
    1. Advertising

  3. skrev:

    > how can I extract 2 integers from a string in python?
    >
    > for example, my source string is this:
    > Total size: 173233 (371587)
    >
    > I want to extract the integer 173233 and 371587 from that
    > soource string, how can I do that?


    E.g.:

    #v+

    >>> import re
    >>> re.findall(r'\d+', 'Total size: 173233 (371587)')

    ['173233', '371587']
    >>>


    #v-

    Mvh,

    --
    Klaus Alexander Seistrup
    Copenhagen, Denmark
    http://surdej.dk/
     
    Klaus Alexander Seistrup, Jun 9, 2006
    #3
  4. Stephen Prinster <> wrote:
    >>>> a, b, c, d = 'Total size: 173233 (371857)'.split()
    >>>> first_int, second_int = int(c), int(d[1:-1])


    int(d[1:-1]) can be replaced by d.strip("()"), which may or
    may not be clearer in intent.

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    ___ | "Frankly I have no feelings towards penguins one way or the other"
    \X/ | -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
     
    Sion Arrowsmith, Jun 9, 2006
    #4
  5. In article <e6b02t$2u2$>,
    Klaus Alexander Seistrup <> wrote:
    > skrev:
    >
    >> how can I extract 2 integers from a string in python?
    >>
    >> for example, my source string is this:
    >> Total size: 173233 (371587)
    >>
    >> I want to extract the integer 173233 and 371587 from that
    >> soource string, how can I do that?

    >
    >E.g.:
    >
    >#v+
    >
    >>>> import re
    >>>> re.findall(r'\d+', 'Total size: 173233 (371587)')

    >['173233', '371587']
    >>>>

    .
    .
    .
    Nicely expressed.

    People doing a lot of report-scraping might also want to
    consider one of the scanf implementations for Python.
    <URL: http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/ >
    has an example.
     
    Cameron Laird, Jun 9, 2006
    #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. R
    Replies:
    2
    Views:
    529
    Jon A. Cruz
    Jul 25, 2003
  2. Replies:
    4
    Views:
    970
    Malte
    Mar 3, 2005
  3. Mike Vallely

    integers as a string

    Mike Vallely, Nov 10, 2003, in forum: C++
    Replies:
    3
    Views:
    417
    Tim Threlfall
    Nov 10, 2003
  4. Dwight Army of Champions
    Replies:
    0
    Views:
    406
    Dwight Army of Champions
    Apr 20, 2010
  5. Jochen Brenzlinger
    Replies:
    5
    Views:
    691
    Daniel Pitts
    Nov 21, 2011
Loading...

Share This Page