Strings in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johny

    #1

    Strings in Python

    Playing a little more with strings, I found out that string.find
    function provides the position of
    the first occurance of the substring in the string.
    Is there a way how to find out all substring's position ?
    To explain more,
    let's suppose

    mystring='12341 '
    import string
    >>string.find(m ystring ,'1')
    0

    But I need to find the possition the other '1' in mystring too.
    Is it possible?
    Or must I use regex?
    Thanks for help
    L

  • Gary Herron

    #2
    Re: Strings in Python

    Johny wrote:
    Playing a little more with strings, I found out that string.find
    function provides the position of
    the first occurance of the substring in the string.
    Is there a way how to find out all substring's position ?
    To explain more,
    let's suppose
    >
    mystring='12341 '
    import string
    >
    >
    >>>string.find( mystring ,'1')
    >>>>
    0
    >
    But I need to find the possition the other '1' in mystring too.
    Is it possible?
    Or must I use regex?
    Thanks for help
    L
    >
    >
    You could use a regular expression. The re module has s function
    "findall" that does what you want.

    Also, if you read the documentation for strings find method, you'll find:

    1 S.find(sub [,start [,end]]) -int
    2
    3 Return the lowest index in S where substring sub is found,
    4 such that sub is contained within s[start,end]. Optional
    5 arguments start and end are interpreted as in slice notation.
    6
    7 Return -1 on failure.

    So put your find in a loop, starting the search one past the previously
    found occurrence.

    i = string.find(mys tring, i+1)

    Gary Herron


    Comment

    • Shawn Milo

      #3
      Re: Strings in Python

      On 8 Feb 2007 08:28:25 -0800, Johny <python@hope.cz wrote:
      Playing a little more with strings, I found out that string.find
      function provides the position of
      the first occurance of the substring in the string.
      Is there a way how to find out all substring's position ?
      To explain more,
      let's suppose
      >
      mystring='12341 '
      import string
      >
      >string.find(my string ,'1')
      0
      >
      But I need to find the possition the other '1' in mystring too.
      Is it possible?
      Or must I use regex?
      Thanks for help
      L
      >
      --

      >
      Loop it -- once you know the index of the first character, add the
      third argument to string.find(), which tells it the position at which
      to start (the last find + 1).

      Comment

      • Shawn Milo

        #4
        Re: Strings in Python

        On 2/8/07, Gary Herron <gherron@island training.comwro te:
        Johny wrote:
        Playing a little more with strings, I found out that string.find
        function provides the position of
        the first occurance of the substring in the string.
        Is there a way how to find out all substring's position ?
        To explain more,
        let's suppose

        mystring='12341 '
        import string

        >>string.find(m ystring ,'1')
        >>>
        0

        But I need to find the possition the other '1' in mystring too.
        Is it possible?
        Or must I use regex?
        Thanks for help
        L
        You could use a regular expression. The re module has s function
        "findall" that does what you want.
        >
        Also, if you read the documentation for strings find method, you'll find:
        >
        1 S.find(sub [,start [,end]]) -int
        2
        3 Return the lowest index in S where substring sub is found,
        4 such that sub is contained within s[start,end]. Optional
        5 arguments start and end are interpreted as in slice notation.
        6
        7 Return -1 on failure.
        >
        So put your find in a loop, starting the search one past the previously
        found occurrence.
        >
        i = string.find(mys tring, i+1)
        >
        Gary Herron
        >
        >
        --

        >
        Speaking of regex examples, that's basically what I did in the script
        below which James Kim and I were collaborating on yesterday and this
        morning, as a result of his thread.

        This matches not only a string, but a regex, then loops through each
        match to do something to it. I hope this helps. I submitted this to
        the list for recommendations on how to make it more Pythonic, but at
        least it works.

        Here are the most important, stripped down pieces:

        #! /usr/bin/python

        import re

        #match a date in this format: 05/MAR/2006
        regex = re.compile(r",\ d{2}/[A-Z]{3}/\d{4},")

        for line in infile:

        matches = regex.findall(l ine)
        for someDate in matches:

        newDate = #do something here
        line = line.replace(so meDate, newDate)


        Here is the full script:

        #! /usr/bin/python

        import sys
        import re

        month ={'JAN':1,'FEB' :2,'MAR':3,'APR ':4,'MAY':5,'JU N':6,'JUL':7,'A UG':8,'SEP':9,' OCT':10,'NOV':1 1,'DEC':12}
        infile=file('TV A-0316','r')
        outfile=file('t mp.out','w')

        def formatDatePart( x):
        "take a number and transform it into a two-character string,
        zero padded"
        x = str(x)
        while len(x) < 2:
        x = "0" + x
        return x

        regex = re.compile(r",\ d{2}/[A-Z]{3}/\d{4},")

        for line in infile:
        matches = regex.findall(l ine)
        for someDate in matches:

        dayNum = formatDatePart( someDate[1:3])
        monthNum = formatDatePart( month[someDate[4:7]])
        yearNum = formatDatePart( someDate[8:12])

        newDate = ",%s-%s-%s," % (yearNum,monthN um,dayNum)
        line = line.replace(so meDate, newDate)

        outfile.writeli nes(line)

        infile.close
        outfile.close

        Comment

        • attn.steven.kuo@gmail.com

          #5
          Re: Strings in Python

          On Feb 8, 8:28 am, "Johny" <pyt...@hope.cz wrote:
          Playing a little more with strings, I found out that string.find
          function provides the position of
          the first occurance of the substring in the string.
          Is there a way how to find out all substring's position ?
          To explain more,
          let's suppose
          >
          mystring='12341 '
          import string
          >
          >string.find(my string ,'1')
          >
          0
          >
          But I need to find the possition the other '1' in mystring too.
          Is it possible?
          Or must I use regex?

          In this case, you can use:

          mystring = '12341'
          indices = [ _ for _ in range(len(mystr ing)) if mystring[_] == '1' ]
          print indices

          --
          Hope this helps,
          Steven

          Comment

          • MRAB

            #6
            Re: Strings in Python

            On Feb 8, 6:32 pm, attn.steven.... @gmail.com wrote:
            On Feb 8, 8:28 am, "Johny" <pyt...@hope.cz wrote:
            >
            >
            >
            Playing a little more with strings, I found out that string.find
            function provides the position of
            the first occurance of the substring in the string.
            Is there a way how to find out all substring's position ?
            To explain more,
            let's suppose
            >
            mystring='12341 '
            import string
            >
            >>string.find(m ystring ,'1')
            >
            0
            >
            But I need to find the possition the other '1' in mystring too.
            Is it possible?
            Or must I use regex?
            >
            In this case, you can use:
            >
            mystring = '12341'
            indices = [ _ for _ in range(len(mystr ing)) if mystring[_] == '1' ]
            print indices
            >
            Or:

            mystring = '12341'
            indices = [ i for i, c in enumerate(mystr ing) if c == '1' ]
            print indices

            Comment

            • Johny

              #7
              Re: Strings in Python

              Thanks ALL for help and ideas
              L

              Comment

              Working...