[Tutor] re help
Ron Nixon
nixonron at yahoo.com
Mon Mar 14 06:26:08 CET 2005
The following program takes text data like this:
Jimi Hendrix
2100 South Ave
Seattle, WA 55408
and changes it to this
Jimi Hendrix, 2100 South Ave,Seattle,WA,55488
and writes it to a file. The problem I'm running into
is that it only writes this first address to a file
and there are several others in the file. I believe it
has something to do with using re.search instead of
re.findall. But re.findall returns a error when I try
using it. Suggestions? Thanks in advance.
Here is the script:
import re
f = open('reformat.txt').read()
pat = re.compile(r"([^\r\n]+)\n([^\r\n]*)\n([^\r\n]*)
([^\r\n]*) ([^\r\n]*)")
x=re.search(pat,f)
name = x.group(1)
address = x.group(2)
citystate = x.group(3)+x.group(4)
zipcd = x.group(5)
o= open('reformat1.txt','w')
o.write("%s,%s,%s,%s\n" % (name, address,
citystate,zipcd))
o.close()
print("%s,%s,%s,%s\n" % (name, address, citystate,zipcd))
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
More information about the Tutor
mailing list