[pyar] Seleccionar elementos de un txt

Terry Reedy tjreedy en udel.edu
Lun Feb 6 19:39:03 ART 2012


On 1/31/2012 11:04 AM, Javier Castrillo wrote:

> f = open("wordlist.txt", "r")
> n = open("wordlist_nuevo.txt", "w")
> pattern = re.compile("^\w+$")
>
> while True:
>      palabra = f.readline()
>      if not palabra:
>          break
>if pattern.match(palabra):
 >          n.write(palabra)
> f.close()
> n.close()

Creo que esto funciona bien (2.7, 3.2):

pattern = re.compile("^\w+$")
with open("wordlist.txt", "r") as f,\
      open("wordlist_nuevo.txt", "w") as n:
     for palabra in f:
         if pattern.match(palabra):
             n.write(palabra)

-- 
Terry Jan Reedy




More information about the pyar mailing list