Extract a number from a complicated string

Discussion in 'Python' started by Horacius ReX, Dec 21, 2007.

  1. Horacius ReX

    Horacius ReX Guest

    Hi,

    I have to read some data from a file, and on each block it always
    appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
    with different numbers like;

    xyz.vs.1-81_1
    xyz.vs.1-1234_1
    xyz.vs.1-56431_1

    and so on

    My problem is that I need to extract from this string the number. For
    instance in xyz.vs.1-81_1 I have to extract the number 81, and in
    xyz.vs.1-1234_1 I need to get the number 1234.

    What is the easiest way of doing this ?

    Thanks
     
    Horacius ReX, Dec 21, 2007
    #1
    1. Advertising

  2. Horacius ReX a écrit :
    > Hi,
    >
    > I have to read some data from a file, and on each block it always
    > appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
    > with different numbers like;
    >
    > xyz.vs.1-81_1
    > xyz.vs.1-1234_1
    > xyz.vs.1-56431_1
    >
    > and so on
    >
    > My problem is that I need to extract from this string the number. For
    > instance in xyz.vs.1-81_1 I have to extract the number 81, and in
    > xyz.vs.1-1234_1 I need to get the number 1234.
    >
    > What is the easiest way of doing this ?


    Not necessarily the "easiest", but:


    >>> data = ['xyz.vs.1-81_1', 'xyz.vs.1-1234_1', 'xyz.vs.1-56431_1']
    >>> for d in data:

    .... print d, ":", d.split('.')[2].split('-')[1].split('_')[0]
    ....
    xyz.vs.1-81_1 : 81
    xyz.vs.1-1234_1 : 1234
    xyz.vs.1-56431_1 : 56431
     
    Bruno Desthuilliers, Dec 21, 2007
    #2
    1. Advertising

  3. Horacius ReX wrote:

    >Hi,
    >
    >I have to read some data from a file, and on each block it always
    >appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
    >with different numbers like;
    >
    >xyz.vs.1-81_1
    >xyz.vs.1-1234_1
    >xyz.vs.1-56431_1
    >
    >and so on
    >
    >My problem is that I need to extract from this string the number. For
    >instance in xyz.vs.1-81_1 I have to extract the number 81, and in
    >xyz.vs.1-1234_1 I need to get the number 1234.
    >
    >What is the easiest way of doing this ?
    >
    >Thanks
    >
    >

    If the strings looks *allways* that way, so its not to complicated:
    >>> number_regex = re.compile('-(\d+)_')
    >>> print number_regex.search('xyz.vs.1-1234_1').group(1)

    1234
    >>> print number_regex.search('xyz.vs.1-56431_1').group(1)

    56431
     
    Gerardo Herzig, Dec 21, 2007
    #3
  4. Horacius ReX

    Neil Cerutti Guest

    On 2007-12-21, Horacius ReX <> wrote:
    > Hi,
    >
    > I have to read some data from a file, and on each block it always
    > appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
    > with different numbers like;
    >
    > xyz.vs.1-81_1
    > xyz.vs.1-1234_1
    > xyz.vs.1-56431_1
    >
    > and so on
    >
    > My problem is that I need to extract from this string the number. For
    > instance in xyz.vs.1-81_1 I have to extract the number 81, and in
    > xyz.vs.1-1234_1 I need to get the number 1234.
    >
    > What is the easiest way of doing this ?


    Using a regular expression would be quick if you know how.

    Or use str.find and slicing.

    --
    Neil Cerutti
     
    Neil Cerutti, Dec 21, 2007
    #4
  5. Gerardo Herzig <> wrote:
    >>My problem is that I need to extract from this string the number. For
    >>instance in xyz.vs.1-81_1 I have to extract the number 81, and in
    >>xyz.vs.1-1234_1 I need to get the number 1234.
    >>
    >>What is the easiest way of doing this ?

    >If the strings looks *allways* that way, so its not to complicated:
    > >>> number_regex = re.compile('-(\d+)_')

    > [ ... ]


    If the strings always look *exactly* like that, a regex is a massive
    overkill:

    >>> examples = 'xyz.vs.1-81_1', 'xyz.vs.1-1234_1', 'xyz.vs.1-56431_1'
    >>> [ int(x[9:-2]) for x in examples ]

    [81, 1234, 56431]

    If the "xyz.vs.1" and trailing "1" are a bit more variable, then a
    regex is still massive overkill:

    >>> [ int(x[x.index('-')+1:x.rindex('_')]) for x in examples ]

    [81, 1234, 56431]

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
     
    Sion Arrowsmith, Dec 21, 2007
    #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. Horacius ReX

    Complicated string substitution

    Horacius ReX, Feb 13, 2008, in forum: Python
    Replies:
    1
    Views:
    225
    Tim Chase
    Feb 14, 2008
  2. Dwight Army of Champions
    Replies:
    0
    Views:
    408
    Dwight Army of Champions
    Apr 20, 2010
  3. Jochen Brenzlinger
    Replies:
    5
    Views:
    693
    Daniel Pitts
    Nov 21, 2011
  4. Adam

    Extract a number from a string.

    Adam, Nov 8, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    166
    Ben Morrow
    Nov 11, 2004
  5. Looden

    extract number from binary string

    Looden, Mar 1, 2010, in forum: Perl Misc
    Replies:
    6
    Views:
    204
Loading...

Share This Page