473,387 Members | 1,541 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.

parse data

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!

Nov 9 '05 #1
5 1459
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:
listy = stringy.split(",")
print listy

[' Happy Happy Cow', ' 50', " 1234 Your Mom's House AllTheTime", 'USA
']
Hope this helps!

Nov 9 '05 #2
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:
listy = stringy.split(",")
print listy

[' Happy Happy Cow', ' 50', " 1234 Your Mom's House AllTheTime", 'USA
']
Hope this helps!

Nov 9 '05 #3
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
Nov 9 '05 #4
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 md*@micah.elliott.name
" " """
Nov 9 '05 #5
py wrote:
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
Nov 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: H.L Bai | last post by:
hi, everybody i meet a parse error when i used the xml4c. any proposal is helpful. The error is following .../XMLRegionHandler.h:59 parse error before '*' .../XMLRegionHandler.h:60 parse...
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
8
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and I need to parse strings that look similar to the one below. All 5 rows will make up one string. I have a form where a use can copy/paste data like what you...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
2
by: Samuel R. Neff | last post by:
I'm using a quasi open-source project and am running into an exception in double.Parse which is effectively this: double.Parse(double.MinValue.ToString()) System.OverflowException: Value was...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
1
by: =?Utf-8?B?YXZucmFv?= | last post by:
We have a web service that gets data in xml format. In that Xml data, we parse few date fields that are in this format <data datefield="12/26/2008" timefield="16:33:45" ...> we parse it into a...
1
by: rajc_144 | last post by:
i am doing some research where i need to parse some data from SEC web site. the data is not in xml format and sort of unstructured. can someone recommand me a way to parse this data. i need to...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.