[Tutor] Regular Expression

Tiger12506 keridee at jayco.net
Sat Dec 22 05:19:51 CET 2007


>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}])")

You have to allow for the characters at the beginning and end too.
Try this.
re.compile(r".*~02(\d{10})~.*")

Also outfile.write("%s\n") literally writes "%s\n"
You need this I believe

outfile.write("%s\n" % m.group(1))


> 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
> 



More information about the Tutor mailing list