Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80446

Re: Python script that does batch find and replace in txt files

Date 2014-11-09 14:58 -0800
From Albert-Jan Roskam <fomcl@yahoo.com>
Subject Re: Python script that does batch find and replace in txt files
Newsgroups comp.lang.python
Message-ID <mailman.15612.1415574059.18130.python-list@python.org> (permalink)

Show all headers | View raw


----------------------------
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()]

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Python script that does batch find and replace in txt files Albert-Jan Roskam <fomcl@yahoo.com> - 2014-11-09 14:58 -0800
  Re: Python script that does batch find and replace in txt files Syed Khalid <khalidness@gmail.com> - 2014-11-09 17:49 -0800
    Re: Python script that does batch find and replace in txt files alister <alister.nospam.ware@ntlworld.com> - 2014-11-10 11:28 +0000

csiph-web