extracting a substring

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

    #1

    extracting a substring

    Hi,
    I have a bunch of strings like
    a53bc_531.txt
    a53bc_2285.txt
    ....
    a53bc_359.txt

    and I want to extract the numbers 531, 2285, ...,359.

    One thing for sure is that these numbers are the ONLY part that is
    changing; all the other characters are always fixed.

    I know I should use regular expressions, but I'm not familar with
    python, so any quick help would help, such as which commands or idioms
    to use. Thanks a lot!

  • Felipe Almeida Lessa

    #2
    Re: extracting a substring

    Em Ter, 2006-04-18 às 17:25 -0700, b83503104@yahoo .com escreveu:[color=blue]
    > Hi,
    > I have a bunch of strings like
    > a53bc_531.txt
    > a53bc_2285.txt
    > ...
    > a53bc_359.txt
    >
    > and I want to extract the numbers 531, 2285, ...,359.[/color]

    Some ways:

    1) Regular expressions, as you said:[color=blue][color=green][color=darkred]
    >>> from re import compile
    >>> find = compile("a53bc_ ([1-9]*)\\.txt").find all
    >>> find('a53bc_531 .txt\na53bc_228 5.txt\na53bc_35 9.txt')[/color][/color][/color]
    ['531', '2285', '359']

    2) Using ''.split:[color=blue][color=green][color=darkred]
    >>> [x.split('.')[0].split('_')[1] for x in 'a53bc_531.txt[/color][/color][/color]
    \na53bc_2285.tx t\na53bc_359.tx t'.splitlines()]
    ['531', '2285', '359']

    3) Using indexes (be careful!):[color=blue][color=green][color=darkred]
    >>> [x[6:-4] for x in 'a53bc_531.txt\ na53bc_2285.txt[/color][/color][/color]
    \na53bc_359.txt '.splitlines()]
    ['531', '2285', '359']

    Measuring speeds:

    $ python2.4 -m timeit -s 'from re import compile; find =
    compile("a53bc_ ([1-9]*)\\.txt").find all; s = "a53bc_531. txt
    \na53bc_2285.tx t\na53bc_359.tx t"' 'find(s)'
    100000 loops, best of 3: 3.03 usec per loop

    $ python2.4 -m timeit -s 's = "a53bc_531.txt\ na53bc_2285.txt
    \na53bc_359.txt \n"[:-1]' "[x.split('.')[0].split('_')[1] for x in
    s.splitlines()]"
    100000 loops, best of 3: 7.64 usec per loop

    $ python2.4 -m timeit -s 's = "a53bc_531.txt\ na53bc_2285.txt
    \na53bc_359.txt \n"[:-1]' "[x[6:-4] for x in s.splitlines()]"
    100000 loops, best of 3: 2.47 usec per loop


    $ python2.4 -m timeit -s 'from re import compile; find =
    compile("a53bc_ ([1-9]*)\\.txt").find all; s = ("a53bc_531. txt
    \na53bc_2285.tx t\na53bc_359.tx t\n"*1000)[:-1]' 'find(s)'
    1000 loops, best of 3: 1.95 msec per loop

    $ python2.4 -m timeit -s 's = ("a53bc_531.txt \na53bc_2285.tx t
    \na53bc_359.txt \n" * 1000)[:-1]' "[x.split('.')[0].split('_')[1] for x
    in s.splitlines()]"
    100 loops, best of 3: 6.51 msec per loop

    $ python2.4 -m timeit -s 's = ("a53bc_531.txt \na53bc_2285.tx t
    \na53bc_359.txt \n" * 1000)[:-1]' "[x[6:-4] for x in s.splitlines()]"
    1000 loops, best of 3: 1.53 msec per loop


    Summary: using indexes is less powerful than regexps, but faster.

    HTH,

    --
    Felipe.

    Comment

    • Gary Herron

      #3
      Re: extracting a substring

      b83503104@yahoo .com wrote:
      [color=blue]
      >Hi,
      >I have a bunch of strings like
      >a53bc_531.tx t
      >a53bc_2285.t xt
      >...
      >a53bc_359.tx t
      >
      >and I want to extract the numbers 531, 2285, ...,359.
      >
      >One thing for sure is that these numbers are the ONLY part that is
      >changing; all the other characters are always fixed.
      >
      >I know I should use regular expressions, but I'm not familar with
      >python, so any quick help would help, such as which commands or idioms
      >to use. Thanks a lot!
      >
      >
      >[/color]
      Try this:
      [color=blue][color=green][color=darkred]
      >>> import re
      >>> pattern = re.compile("a53 bc_([0-9]*).txt")
      >>>
      >>> s = "a53bc_531. txt"
      >>> match = pattern.match(s )
      >>> if match:[/color][/color][/color]
      .... print int(match.group (1))
      .... else:
      .... print "No match"
      ....
      531[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Hope that helps,
      Gary Herron


      Comment

      • Dale Strickland-Clark

        #4
        Re: extracting a substring

        You don't need a regex for this, as long as the prefix and suffix are fixed
        lengths, the following will do:
        [color=blue][color=green]
        >> "a53bc_531. txt"[6:-4][/color][/color]
        '531'[color=blue][color=green][color=darkred]
        >>> "a53bc_2285.txt "[6:-4][/color][/color][/color]
        '2285'



        b83503104@yahoo .com wrote:
        [color=blue]
        > Hi,
        > I have a bunch of strings like
        > a53bc_531.txt
        > a53bc_2285.txt
        > ...
        > a53bc_359.txt
        >
        > and I want to extract the numbers 531, 2285, ...,359.
        >
        > One thing for sure is that these numbers are the ONLY part that is
        > changing; all the other characters are always fixed.
        >
        > I know I should use regular expressions, but I'm not familar with
        > python, so any quick help would help, such as which commands or idioms
        > to use. Thanks a lot![/color]

        --
        Dale Strickland-Clark
        Riverhall Systems - www.riverhall.co.uk

        Comment

        • Kent Johnson

          #5
          Re: extracting a substring

          b83503104@yahoo .com wrote:[color=blue]
          > Hi,
          > I have a bunch of strings like
          > a53bc_531.txt
          > a53bc_2285.txt
          > ...
          > a53bc_359.txt
          >
          > and I want to extract the numbers 531, 2285, ...,359.
          >
          > One thing for sure is that these numbers are the ONLY part that is
          > changing; all the other characters are always fixed.[/color]

          In that case a fixed slice will do what you want:

          In [1]: s='a53bc_531.tx t'

          In [2]: s[6:-4]
          Out[2]: '531'

          Kent

          Comment

          • rx

            #6
            Re: extracting a substring

            [color=blue]
            > and I want to extract the numbers 531, 2285, ...,359.
            >
            > One thing for sure is that these numbers are the ONLY part that is
            > changing; all the other characters are always fixed.
            >[/color]

            I'm not sure about what you mean by "always fixed" but I guess it means that
            you have n files with a fixed start and a changing ending, and m files with
            a fixed start and a changing ending, ....

            import re
            filenames=['ac99_124.txt', 'ac99_344.txt', 'ac99_445.txt']
            numbers=[]
            for i in filenames:
            numbers.append( int(re.compile( '[^_]*_(?P<number>[^.]*).txt').match( i).group('numbe r')))



            this sets numbers to: [124, 344, 445]


            Comment

            Working...