parse data

Micah Elliott mde at micah.elliott.name
Wed Nov 9 12:26:35 EST 2005


On Nov 09, Dennis Benzinger wrote:
> 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

Dennis' solution is correct.  If you want to avoid REs, and concision
and speed are premiums, then you might refine it to:

names = [line[5:].strip() for line in your_data.split('\n')
         if line.startswith('Name:')]

-- 
_ _     ___
|V|icah |- lliott  http://micah.elliott.name  mde at micah.elliott.name
" "     """



More information about the Python-list mailing list