Python script that does batch find and replace in txt files

Albert-Jan Roskam fomcl at yahoo.com
Sun Nov 9 23:58:23 CET 2014


----------------------------
On Sun, Nov 9, 2014 10:51 PM CET Syed Khalid wrote:

>Albert,
>
>Thanks a million for script,
>
>It worked fine after I closed the bracket.
>
>
>import glob, codecs, re, os
>
>regex = re.compile(r"Age: |Sex: |House No:  ") # etc etc
>
>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 "Elector's Name:" in line:
>            break
>    newlines = [regex.sub(", line).strip().replace("-", "_") for line in
>oldlines[i:]]

You're welcome

newlines = [regex.sub("", line).strip().replace("-", "_") for line in
oldlines[i:] if line.strip()]



More information about the Python-list mailing list