Parsing text

Gerard Flanagan grflanagan at yahoo.co.uk
Tue Dec 20 12:25:58 EST 2005


sicvic wrote:

> Since I cant show the actual output file lets say I had an output file
> that looked like this:
>
> aaaaa bbbbb Person: Jimmy
> Current Location: Denver

It may be the output of another process but it's the input file as far
as the parsing code is concerned.

The code below gives the following output, if that's any help ( just
adapting Noah's idea above).  Note that it deals with the input as a
single string rather than line by line.


Jimmy
Jimmy.txt

Current Location: Denver
Next Location: Chicago

Sarah
Sarah.txt

Current Location: San Diego
Next Location: Miami
Next Location: New York

>>>

data='''
aaaaa bbbbb Person: Jimmy
Current Location: Denver
Next Location: Chicago
----------------------------------------------
aaaaa bbbbb Person: Sarah
Current Location: San Diego
Next Location: Miami
Next Location: New York
----------------------------------------------
'''

import StringIO
import re


src = StringIO.StringIO(data)

for name in ['Jimmy', 'Sarah']:
    exp = "(?s)Person: %s(.*?)--" % name
    filename = "%s.txt" % name
    info = re.findall(exp, src.getvalue())[0]
    print name
    print filename
    print info



hth

Gerard




More information about the Python-list mailing list