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.
Hameed has shown you one solution. I would like to point out that if you plan to read this data back in to a program, the format you have chosen is problematic. You can't count on the number of commas being fixed. For example, the address
John Doe, Sr.
2100 South Ave
Seattle WA 55408
would become John Doe, Sr.,2100 South Ave,Seattle WA,55408
If you try to split this at the commas you will not get the correct result. One solution is to use the csv module which will quote the strings containing commas.
Kent
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/ _______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor