473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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=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
4 1639
ghostdog74
511 Expert 256MB
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
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
2,851 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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

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

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.