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


Groups > comp.lang.python > #80446 > unrolled thread

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

Started byAlbert-Jan Roskam <fomcl@yahoo.com>
First post2014-11-09 14:58 -0800
Last post2014-11-10 11:28 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

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

FromAlbert-Jan Roskam <fomcl@yahoo.com>
Date2014-11-09 14:58 -0800
SubjectRe: Python script that does batch find and replace in txt files
Message-ID<mailman.15612.1415574059.18130.python-list@python.org>
----------------------------
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()]

[toc] | [next] | [standalone]


#80448

FromSyed Khalid <khalidness@gmail.com>
Date2014-11-09 17:49 -0800
Message-ID<a8e23ec0-8642-4fa5-b9be-3dc4c214a9a6@googlegroups.com>
In reply to#80446
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

[toc] | [prev] | [next] | [standalone]


#80459

Fromalister <alister.nospam.ware@ntlworld.com>
Date2014-11-10 11:28 +0000
Message-ID<sj18w.682042$9R5.192410@fx29.am4>
In reply to#80448
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web