parse data

Larry Bates larry.bates at websafe.com
Thu Nov 10 01:40:05 CET 2005


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



More information about the Python-list mailing list