[Tutor] re help
Kent Johnson
kent37 at tds.net
Mon Mar 14 12:11:05 CET 2005
Ron Nixon wrote:
> 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list