How to extract 2 integers from a string in python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yinglcs@gmail.com

    #1

    How to extract 2 integers from a string in python?

    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?

  • Stephen Prinster

    #2
    Re: How to extract 2 integers from a string in python?

    yinglcs@gmail.c om wrote:[color=blue]
    > 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?
    >[/color]

    Use split() to split the string into four strings, using spaces as
    separators, then use int() to convert the resulting strings that
    interest you.
    [color=blue][color=green][color=darkred]
    >>> a, b, c, d = 'Total size: 173233 (371857)'.split ()
    >>> first_int, second_int = int(c), int(d[1:-1])
    >>> first_int[/color][/color][/color]
    173233[color=blue][color=green][color=darkred]
    >>> second_int[/color][/color][/color]
    371857

    HTH
    Steve P

    Comment

    • Klaus Alexander Seistrup

      #3
      Re: How to extract 2 integers from a string in python?

      yinglcs@gmail.c om skrev:
      [color=blue]
      > 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?[/color]

      E.g.:

      #v+
      [color=blue][color=green][color=darkred]
      >>> import re
      >>> re.findall(r'\d +', 'Total size: 173233 (371587)')[/color][/color][/color]
      ['173233', '371587'][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      #v-

      Mvh,

      --
      Klaus Alexander Seistrup
      Copenhagen, Denmark

      Comment

      • Sion Arrowsmith

        #4
        Re: How to extract 2 integers from a string in python?

        Stephen Prinster <prinster@mail. com> wrote:[color=blue][color=green][color=darkred]
        >>>> a, b, c, d = 'Total size: 173233 (371857)'.split ()
        >>>> first_int, second_int = int(c), int(d[1:-1])[/color][/color][/color]

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

        --
        \S -- siona@chiark.gr eenend.org.uk -- 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

        Comment

        • Cameron Laird

          #5
          Re: How to extract 2 integers from a string in python?

          In article <e6b02t$2u2$1@m inji.szn.dk>,
          Klaus Alexander Seistrup <klaus@seistrup .dk> wrote:[color=blue]
          >yinglcs@gmail. com skrev:
          >[color=green]
          >> 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?[/color]
          >
          >E.g.:
          >
          >#v+
          >[color=green][color=darkred]
          >>>> import re
          >>>> re.findall(r'\d +', 'Total size: 173233 (371587)')[/color][/color]
          >['173233', '371587'][color=green][color=darkred]
          >>>>[/color][/color][/color]

          Comment

          Working...