parse data

P

py

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!
 
M

MooMaster

If you know the indices of where the data should be in your string, you
can use substrings... ie:
stringy = " Happy Happy Cow, 50, 1234 Your Mom's House AllTheTime,USA "
stringy[0:16]
' Happy Happy Cow'

If the data isn't set all the time (for example, and address doesn't
have a mandatory length), then you're probably stuck using the index
function...unless you have everything separated by a delimiter, such as
a ","...then this would work:
[' Happy Happy Cow', ' 50', " 1234 Your Mom's House AllTheTime", 'USA
']


Hope this helps!
 
M

MooMaster

If you know the indices of where the data should be in your string, you
can use substrings... ie:
stringy = " Happy Happy Cow, 50, 1234 Your Mom's House AllTheTime,USA "
stringy[0:16]
' Happy Happy Cow'

If the data isn't set all the time (for example, and address doesn't
have a mandatory length), then you're probably stuck using the index
function...unless you have everything separated by a delimiter, such as
a ","...then this would work:
[' Happy Happy Cow', ' 50', " 1234 Your Mom's House AllTheTime", 'USA
']


Hope this helps!
 
D

Dennis Benzinger

py said:
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
 
M

Micah Elliott

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:')]
 
L

Larry Bates

py said:
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!
Something like this works if line spacing can be depended on.
Also a good way to hide the actual format of the string from your
main program.

Larry Bates

class personClass:
def __init__(self, nameline, ageline):
self.name=nameline.split(':')[1].strip()
self.age=int(ageline.split(':')[1].strip())
return

class peopleClass:
def __init__(self, initialstring):
#
# Define a list where I can store people
#
self.peoplelist=[]
self.next_index=0
#
# Split the initial string on newlines
#
lines=initialstring.split('\n')
#
# Loop over the lines separating the people out
#
while 1:
lines.pop(0) # Throw away the person number line
bl1=lines.pop(0) # Throw away the blank line
nameline=lines.pop(0) # Get name line
ageline=lines.pop(0) # Get age line
#
# Create person instance and append to peoplelist
#
self.peoplelist.append(personClass(nameline, ageline))
try: bl2=lines.pop(0) # Throw away trailing blank line 1
except: break # All done if there is none
try: bl3=lines.pop(0) # Throw away trailing blank line 2
except: break # All done if there is none

return

def __len__(self):
return len(self.peoplelist)

def __iter__(self):
return self

def next(self):
#
# Try to get the next person
#
try: PERSON=self.peoplelist[self.next_index]
except:
self.next_index=0
raise StopIteration
#
# Increment the index pointer for the next call
#
self.next_index+=1
return PERSON

if __name__== "__main__":
initialstring='person number 1\n\nName: bob\nAge: 50\n\n\n' \
'person number 2\n\nName: jim\nAge: 39'
people=peopleClass(initialstring)
for person in people:
print "Name:", person.name
print "Age:", person.age
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,349
Members
48,035
Latest member
SamuelDieng

Latest Threads

Top