parse data

Dennis Benzinger Dennis.Benzinger at gmx.net
Wed Nov 9 11:17:47 EST 2005


py schrieb:
> I have some data (in a string) such as....
> 
> person number 1
> 
> Name: bob
> Age: 50
> 
> 
> person number 2
> 
> Name: jim
> Age: 39
> 
> ...all that is stored in a string.  I need to pull out the names of the
> different people and put them in a list or something.  Any
> suggestions...besides doing data.index("name")...over and over?
> 
> thanks!
> 

Use the re module:


import re

your_data = """person number 1

Name: bob
Age: 50


person number 2

Name: jim
Age: 39"""


names = []

for match in re.finditer("Name:(.*)", your_data):
     names.append(match.group(1))

print names



Bye,
Dennis



More information about the Python-list mailing list