[Tutor] Regular Expression
Michael Langford
mlangford.cs03 at gtalumni.org
Sat Dec 22 05:26:55 CET 2007
You need to pass a parameter to the string in the following line:
outfile.write("%s\n" % m.string[m.start():m.end()])
And you need to use m.search, not m.match in the line where you're
actually apply the expression to the string
m = patt.search(line)
--Michael
On 12/21/07, Que Prime <queprime at gmail.com> wrote:
>
>
> I need to pull the highligted data from a similar file and can't seem to get
> my script to work:
>
> Script:
> import re
> infile = open("filter.txt","r")
> outfile = open("out.txt","w")
> patt = re.compile(r"~02([\d{10}])")
> for line in infile:
> m = patt.match(line)
> if m:
> outfile.write("%s\n")
> infile.close()
> outfile.close()
>
>
> File:
> 200~021111001491~05070
> 200~021111001777~05070
> 200~021111001995~05090
> 200~021111002609~05090
> 200~021111002789~05070
> 200~012~021111004169~0
> 200~021111004247~05090
> 200~021111008623~05090
> 200~021111010957~05090
> 200~02 1111011479~05090
> 200~0199~021111001237~
> 200~021111011600~05090
> 200~012~02 1111022305~0
> 200~021111023546~05090
> 200~021111025427~05090
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
More information about the Tutor
mailing list