Python script that does batch find and replace in txt files

Clayton Kirkwood crk at godblessthe.us
Mon Nov 10 00:03:58 CET 2014



>-----Original Message-----
>From: Python-list [mailto:python-list-
>bounces+crk=godblessthe.us at python.org] On Behalf Of Syed Khalid
>Sent: Sunday, November 09, 2014 1:08 PM
>To: python-list at python.org
>Subject: Re: Python script that does batch find and replace in txt files
>
>My Script,
>
>I have added
>
>import glob, codecs, re, os
>
>regex = re.compile(r"Age: |Sex: |House No: ") # etc etc Script I
>executed in EditRocket :
>
>for txt in glob.glob("/D:/Python/source/*.txt"):
>    with codecs.open(txt, encoding="utf-8") as f:
>        oldlines = f.readlines()
>    for i, line in enumerate(oldlines):
>        if "Contact's Name:" in line:
>            break
>    newlines = [regex.sub("", line).strip().replace("-", "_") for line
>in oldlines[i:]
>    with codecs.open(txt + "_out.txt", "wb", encoding="utf-8") as w:
>        w.write(os.linesep.join(newlines))
>

To my young eyes, it looks like you are missing a ] at the end of the line
before the with.

>
>Error Message :
>
>  File "EamClean.log", line 12
>    with codecs.open(txt + "_out.txt", "wb", encoding="utf-8") as w:
>       ^
>SyntaxError: invalid syntax
>
>Kindly do the needful
>
>
>
>--
>https://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list