Python script that does batch find and replace in txt files
alister
alister.nospam.ware at ntlworld.com
Mon Nov 10 06:28:24 EST 2014
On Sun, 09 Nov 2014 17:49:29 -0800, Syed Khalid wrote:
> 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
kindly read the code to understand how it is operating & then make the
necessary changes/additions yourself comp.lang.python is no a free coding
shop
--
Nobody ever ruined their eyesight by looking at the bright side of
something.
More information about the Python-list
mailing list