Parsing text

2005-12-19 Thread sicvic
I was wondering if theres a way where python can read through the lines
of a text file searching for a key phrase then writing that line and
all lines following it up to a certain point, such as until it sees a
string of "-"

Right now I can only have python write just the line the key phrase is
found in.

Thanks,
Victor

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing text

2005-12-20 Thread sicvic
Not homework...not even in school (do any universities even teach
classes using python?). Just not a programmer. Anyways I should
probably be more clear about what I'm trying to do.

Since I cant show the actual output file lets say I had an output file
that looked like this:

a b Person: Jimmy
Current Location: Denver
Next Location: Chicago
--
a b Person: Sarah
Current Location: San Diego
Next Location: Miami
Next Location: New York
--

Now I want to put (and all recurrences of "Person: Jimmy")

Person: Jimmy
Current Location: Denver
Next Location: Chicago

in a file called jimmy.txt

and the same for Sarah in sarah.txt

The code I currently have looks something like this:

import re
import sys

person_jimmy = open('jimmy.txt', 'w') #creates jimmy.txt
person_sarah = open('sarah.txt', 'w') #creates sarah.txt

f = open(sys.argv[1]) #opens output file
#loop that goes through all lines and parses specified text
for line in f.readlines():
if  re.search(r'Person: Jimmy', line):
person_jimmy.write(line)
elif re.search(r'Person: Sarah', line):
person_sarah.write(line)

#closes all files

person_jimmy.close()
person_sarah.close()
f.close()

However this only would produces output files that look like this:

jimmy.txt:

a b Person: Jimmy

sarah.txt:

a b Person: Sarah

My question is what else do I need to add (such as an embedded loop
where the if statements are?) so the files look like this

a b Person: Jimmy
Current Location: Denver
Next Location: Chicago

and

a b Person: Sarah
Current Location: San Diego
Next Location: Miami
Next Location: New York


Basically I need to add statements that after finding that line copy
all the lines following it and stopping when it sees
'--'

Any help is greatly appreciated.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing text

2005-12-20 Thread sicvic
Thank you everyone!!!

I got a lot more information then I expected. You guys got my brain
thinking in the right direction and starting to like programming.
You've got a great community here. Keep it up.

Thanks,
Victor

-- 
http://mail.python.org/mailman/listinfo/python-list