Python 2.4 searching Data and output in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjawors
    New Member
    • May 2007
    • 2

    #1

    Python 2.4 searching Data and output in a string

    I am trying to to search through a string to find a match and then display everything after the searched text until a comma? Here is an example and where I am stuck.

    my_str = '1=perl,56=python,98=java,12=vbs cript'

    strx = my_str.find("56 =")


    So, I have my start point now which would be = "strx" but I need the data that is actually after the "56=". I am interested in the "python" and would like to output that. It won't work to take the 6 characters after the 56= because that value may be a different length, as a matter of fact will most likely change from string to string. I will be using this procedure to loop through a list that is created from a file that I am opening.

    Any thoughts on this would be much appreciated.

    Am I approaching this wrong?
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by mjawors
    I am trying to to search through a string to find a match and then display everything after the searched text until a comma? Here is an example and where I am stuck.

    my_str = '1=perl,56=python,98=java,12=vbs cript'

    strx = my_str.find("56 =")


    So, I have my start point now which would be = "strx" but I need the data that is actually after the "56=". I am interested in the "python" and would like to output that. It won't work to take the 6 characters after the 56= because that value may be a different length, as a matter of fact will most likely change from string to string. I will be using this procedure to loop through a list that is created from a file that I am opening.

    Any thoughts on this would be much appreciated.

    Am I approaching this wrong?
    one way
    Code:
    my_str = '1=perl,56=python,98=java,12=vbscript'
    for item in my_str.split(","):
       if "56=" in item:
          item=item.replace("56=","")
          print item

    Comment

    • mjawors
      New Member
      • May 2007
      • 2

      #3
      That is one way to complete it but I want to be able to search for that tag. The best bet here is to use a dictionary. I was having a tough time converting the values to an acceptable format but have got it. so here's what I ended up doing:


      my_str = '1=perl,56=pyth on,98=java,12=v bscript'

      did some conversions to get to the right syntax

      my_str = "{'1': 'perl', '56': 'python', '98': 'java', '12': 'vbscript'}"

      >>type(my_str )
      output: <type 'str'>

      >>test = eval(my_str)
      >>type(test)
      output: <type 'dict'>

      >>test['56']
      output: python

      Thanks for your help on this though. I am a newbie just learning my way through this stuff.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by mjawors
        That is one way to complete it but I want to be able to search for that tag. The best bet here is to use a dictionary. I was having a tough time converting the values to an acceptable format but have got it. so here's what I ended up doing:


        my_str = '1=perl,56=pyth on,98=java,12=v bscript'

        did some conversions to get to the right syntax

        my_str = "{'1': 'perl', '56': 'python', '98': 'java', '12': 'vbscript'}"

        >>type(my_str )
        output: <type 'str'>

        >>test = eval(my_str)
        >>type(test)
        output: <type 'dict'>

        >>test['56']
        output: python

        Thanks for your help on this though. I am a newbie just learning my way through this stuff.
        Here's another possibility:[code=Python]import re

        my_str = '1=perl,56=pyth on,98=java,12=v bscript'

        p1 = r'[\d]+'
        p2 = r'(?<==)[^,]+'

        print dict(zip(re.fin dall(p1,my_str) ,re.findall(p2, my_str)))

        '''
        >>> {'1': 'perl', '98': 'java', '12': 'vbscript', '56': 'python'}
        '''[/code]

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Just in case some of the words have numbers:[code=Python]import re

          my_str = '1=perl,56=45py thon,98=j33ava, 12=vbscript'

          p1 = r'[\d]+(?==)'
          p2 = r'(?<==)[^,]+'

          print dict(zip(re.fin dall(p1,my_str) ,re.findall(p2, my_str)))

          '''
          >>> {'1': 'perl', '98': 'j33ava', '12': 'vbscript', '56': '45python'}
          '''[/code]

          Comment

          Working...