Python script that does batch find and replace in txt files
Syed Khalid
khalidness at gmail.com
Sun Nov 9 20:49:29 EST 2014
Albert,
Code is not removing empty lines containing blank characters and not removing leading and trailing spaces present in each line.
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:]]
with codecs.open(txt + "_out.txt", "wb", encoding="utf-8") as w:
w.write(os.linesep.join(newlines))
Kindly do the needful
More information about the Python-list
mailing list