By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Python 2.4 searching Data and output in a string

P: 2
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=vbscript'

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?
May 27 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 511
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=vbscript'

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
Expand|Select|Wrap|Line Numbers
  1. my_str = '1=perl,56=python,98=java,12=vbscript'
  2. for item in my_str.split(","):
  3.    if "56=" in item:
  4.       item=item.replace("56=","")
  5.       print item
  6.  
May 27 '07 #2

P: 2
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=python,98=java,12=vbscript'

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.
May 27 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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=python,98=java,12=vbscript'

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:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. my_str = '1=perl,56=python,98=java,12=vbscript'
  4.  
  5. p1 = r'[\d]+'
  6. p2 = r'(?<==)[^,]+'
  7.  
  8. print dict(zip(re.findall(p1,my_str),re.findall(p2,my_str)))
  9.  
  10. '''
  11. >>> {'1': 'perl', '98': 'java', '12': 'vbscript', '56': 'python'}
  12. '''
May 27 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Just in case some of the words have numbers:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. my_str = '1=perl,56=45python,98=j33ava,12=vbscript'
  4.  
  5. p1 = r'[\d]+(?==)'
  6. p2 = r'(?<==)[^,]+'
  7.  
  8. print dict(zip(re.findall(p1,my_str),re.findall(p2,my_str)))
  9.  
  10. '''
  11. >>> {'1': 'perl', '98': 'j33ava', '12': 'vbscript', '56': '45python'}
  12. '''
May 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.