Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.04; 'syntax': 0.04; 'subject:Python': 0.06; 'automate': 0.07; 'is"': 0.07; '")': 0.09; 'cursor': 0.09; 'executed': 0.09; 'line:': 0.09; 'rows': 0.09; 'subject:files': 0.09; 'subject:script': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'posted': 0.15; '"_")': 0.16; '.txt': 0.16; 'email addr:yahoo.com>': 0.16; 'is"': 0.16; 'newlines': 0.16; 'recorded': 0.16; 'script,': 0.16; 'subject:txt': 0.16; 'syntaxerror:': 0.16; 'the"': 0.16; 'files.': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'skip:g 40': 0.19; '>>>': 0.22; 'select': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'replace': 0.24; 'script.': 0.24; 'skip:l 30': 0.24; 'fine': 0.24; 'mon,': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'script': 0.25; '>': 0.26; 'query': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'am,': 0.29; 'characters': 0.30; 'skip:g 30': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; '"': 0.31; 'subject:that': 0.31; 'txt': 0.31; 'file': 0.32; 'option': 0.32; 'run': 0.32; 'text': 0.33; 'open': 0.33; 'up.': 0.33; 'worked': 0.33; 'url:python': 0.33; '-----': 0.33; 'something': 0.35; 'etc': 0.35; 'received:google.com': 0.35; 'there': 0.35; '8bit%:9': 0.36; 'done': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.37; 'step': 0.37; 'skip:& 10': 0.38; 'thank': 0.38; 'nov': 0.38; 'skip:[ 10': 0.38; 'files': 0.38; 'skip:& 20': 0.39; 'does': 0.39; 'subject:': 0.39; 'delete': 0.39; 'space': 0.40; 'url:mail': 0.40; 'remove': 0.60; 'blank': 0.60; 'deleting': 0.60; 'skip:\xc2 10': 0.60; 'break': 0.61; 'name:': 0.61; 'kindly': 0.61; 'you.': 0.62; 'save': 0.62; 'email name:python-list': 0.65; 'to:addr:gmail.com': 0.65; 'reply': 0.66; 'invalid': 0.68; 'skip:w 30': 0.69; 'containing': 0.69; 'million': 0.74; 'to:addr:yahoo.com': 0.81; '"contact': 0.84; '10:': 0.84; '11:': 0.84; '12:': 0.84; '13:': 0.84; '14:': 0.84; 'no:': 0.84; 'subject:find': 0.84; 'syed': 0.84; '|house': 0.84; 'opens': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=8i0r6+hRXmKVaiFPEUz7ahfzMzQkIdNWZrN4i6zhh80=; b=xDqpZytSBERyJa3b9rN50k4nWfTiMg47VvsQLqeWwmTyKHuyC1NQNeP6ni1Ax9sEfC nNTmJ0gzyzQpl1DM0/cPtUNaC0xUiYMv5IYexSNz7sMnHwWwjE/WvVdjerArDohiDVeK 8RBIdZ2T2Lz4Fut0hmd1p/wcc8g/lEvJMke6oEMY8CXilk0XufuxBMIqllHllnlPmpCd r3YoTOL8Ox9qIomoVSy8+mGm4k/R85ECplArrvvfElpvsxqBkAC5xoXIae+xR00JeKkI XZfrPKbOl/zMJGXVAbdRLiN6rn47yb3lTZL+vfa6HnYngbqLt8WgPwMUNhnfiClGyHB5 QrnA== MIME-Version: 1.0 X-Received: by 10.152.87.171 with SMTP id az11mr25036559lab.37.1415569897350; Sun, 09 Nov 2014 13:51:37 -0800 (PST) In-Reply-To: References: <46769509.90793.1415564600851.JavaMail.yahoo@jws10740.mail.gq1.yahoo.com> Date: Mon, 10 Nov 2014 03:21:37 +0530 Subject: Re: Python script that does batch find and replace in txt files From: Syed Khalid To: Albert-Jan Roskam , Syed Tameem Ahmed Content-Type: multipart/alternative; boundary=001a11c238aee2edac0507740c83 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 314 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415569899 news.xs4all.nl 2881 [2001:888:2000:d::a6]:52876 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80445 --001a11c238aee2edac0507740c83 Content-Type: text/plain; charset=UTF-8 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:]] with codecs.open(txt + "_out.txt", "wb", encoding="utf-8") as w: w.write(os.linesep.join(newlines)) This program is not deleting the empty lines containing blank characters. Kindly do the needful. On Mon, Nov 10, 2014 at 2:50 AM, Syed Khalid wrote: > > Code after adding path of .txt files : > > 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)) > > > I executed code in edit rocket. > > Error message : > > > File "EamClean.log", line 12 > with codecs.open(txt + "_out.txt", "wb", encoding="utf-8") as w: > ^ > SyntaxError: invalid syntax > > > > > > On Mon, Nov 10, 2014 at 2:22 AM, Syed Khalid wrote: > >> Hi Albert, >> >> Thank you for script. >> >> I am getting the below error : >> >> File "EamClean.log", line 12 >> with codecs.open(txt + "_out.txt", "wb", encoding="utf-8") as w: >> ^ >> SyntaxError: invalid syntax >> >> Kindly do the needful. >> >> On Mon, Nov 10, 2014 at 1:53 AM, Albert-Jan Roskam >> wrote: >> >>> >>> >>> >>> >>> ----- Original Message ----- >>> > From: Syed Khalid >>> > To: python-list@python.org >>> > Cc: >>> > Sent: Sunday, November 9, 2014 8:58 PM >>> > Subject: Python script that does batch find and replace in txt files >>> > >>> > Python script that does batch find and replace in txt files Need a >>> python script >>> > that opens all .txt files in a folder find replace/delete text and >>> save files. >>> > >>> > I have text files and I need to perform below steps for each file. >>> > >>> > Step 1: Put cursor at start of file and Search for "Contact's >>> > Name:". Delete all the rows before it. >>> > Step 2: Put cursor at end of file, Search for "Contact's Name:" >>> > select option UP. >>> > Step 3: Search for "Photo of the" Replace with blanks >>> > Step 4: Search for "Contact is" Replace with blanks >>> > Step 5: Search for "Contact's Name:" Replace with blanks >>> > Step 6: Search for "Age:" Replace with blanks >>> > Step 7: Search for "Sex:" Replace with blanks >>> > Step 8: Search for "House No:" Replace with blanks >>> > Step 9: Search for "available" Replace with blanks >>> > Step 10: Remove Empty Lines Containing Blank Characters from file >>> > Step 11: Trim Leading Space for each line >>> > Step 12: Trim Trailing Space after each line >>> > Step 13: Search for - (hyphen) Replace with _ (underscore) >>> >>> > Step 14: Save file. >>> >>> something like (untested) >>> >>> >>> import glob, codecs, re, os >>> >>> regex = re.compile(r"Age: |Sex: |House No: ") # etc etc >>> >>> for txt in glob.glob("/some/path/*.txt"): >>> with codecs.open(txt, encoding="utf-8") as f: >>> oldlines = f.readlines() >>> for i, line in enumerate(oldlines): >>> if "Contact'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)) >>> >>> >>> >>> >>> > >>> > Currently I have recorded a macro in Notepad++. >>> > I open each file, run macro and save file. >>> > As there are many files I was looking for a program to automate the >>> process. >>> > >>> > I posted the same query in Notepad++ forum. I got a reply that it can >>> be done by >>> > using Python script. >>> > >>> > Kindly do the needful. >>> > >>> > Thank you. >>> > khalidness >>> > >>> > -- >>> > https://mail.python.org/mailman/listinfo/python-list >>> > >>> >> >> > --001a11c238aee2edac0507740c83 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Albert,=C2=A0

Thanks a million for scri= pt,=C2=A0

It worked fine after I closed the bracke= t.


import glob, codecs, re, os=

regex =3D re.compile(r"Age: |Sex: |House No:= =C2=A0") # etc etc

for txt in glob.glob(&quo= t;D:/Python/source/*.txt"): =C2=A0=C2=A0
=C2=A0 =C2=A0 with = codecs.open(txt, encoding=3D"utf-8") as f:
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 oldlines =3D f.readlines()
=C2=A0 =C2=A0 for i,= line in enumerate(oldlines):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if &quo= t;Elector's Name:" in line:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 break
=C2=A0 =C2=A0 newlines =3D [regex.sub("&= quot;, line).strip().replace("-", "_") for line in oldl= ines[i:]]
=C2=A0 =C2=A0 with codecs.open(txt + "_out.txt&quo= t;, "wb", encoding=3D"utf-8") as w:
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 w.write(os.linesep.join(newlines))

This program is not deleting the empty lines con= taining blank characters.
Kindly do the needful.


On Mon,= Nov 10, 2014 at 2:50 AM, Syed Khalid <khalidness@gmail.com> wrote:

Code after adding path of .txt files :

import glob, codecs, re, os

rege= x =3D re.compile(r"Age: |Sex: |House No: ") # etc etc
<= br>
for txt in glob.glob("D:/Python/source/*.txt"= ;): =C2=A0=C2=A0
=C2=A0 =C2=A0 with codecs.open(= txt, encoding=3D"utf-8") as f:
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 oldlines =3D f.readlines()
=C2=A0 =C2=A0 for i, line in enume= rate(oldlines):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if "Elect= or's Name:" in line:
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 break
=C2=A0 =C2=A0 newlines =3D [regex.= sub("", line).strip().replace("-", "_") for l= ine in oldlines[i:]
=C2=A0 =C2=A0 with codecs.open(txt + "_o= ut.txt", "wb", encoding=3D"utf-8") as w:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 w.write(os.linesep.join(newlines))


I executed code in edit rocket.

Error message : =C2=A0
<= br>

File "EamClean.log", line 12
=C2=A0 =C2=A0 with codecs.open(txt + "_out.txt", "wb"= ;, encoding=3D"utf-8") as w:
=C2=A0 =C2=A0 =C2=A0 =C2= =A0^
SyntaxError: invalid syntax





On Mon= , Nov 10, 2014 at 2:22 AM, Syed Khalid <khalidness@gmail.com> wrote:
Hi Albert,= =C2=A0

Thank you for script.=C2=A0

<= div>I am getting the below error :

=C2=A0 Fil= e "EamClean.log", line 12
=C2=A0 =C2=A0 with code= cs.open(txt + "_out.txt", "wb", encoding=3D"utf-8&= quot;) as w:
=C2=A0 =C2=A0 =C2=A0 =C2=A0^
Syntax= Error: invalid syntax

Kindly do the needful.=

On Mon, Nov 10, 2014 at 1:53 AM, Albert-Jan Roskam &= lt;fomcl@yahoo.com= > wrote:




----- Original Message -----
> From: Syed Khalid <khalidness@gmail.com>
> To: python= -list@python.org
> Cc:
> Sent: Sunday, November 9, 2014 8:58 PM
> Subject: Python script that does batch find and replace in txt files >
> Python script that does batch find and replace in txt files Need a pyt= hon script
> that opens all .txt files in a folder find replace/delete text and sav= e files.
>
> I have text files and I need to perform below steps for each file.
>
> Step 1: Put cursor at start of file and Search for "Contact's=
> Name:". Delete all the rows before it.
> Step 2: Put cursor at end of file, Search for "Contact's Name= :"
> select option UP.
> Step 3: Search for "Photo of the" Replace with blanks
> Step 4: Search for "Contact is" Replace with blanks
> Step 5: Search for "Contact's Name:" Replace with blanks=
> Step 6: Search for "Age:" Replace with blanks
> Step 7: Search for "Sex:" Replace with blanks
> Step 8: Search for "House No:" Replace with blanks
> Step 9: Search for "available" Replace with blanks
> Step 10: Remove Empty Lines Containing Blank Characters from file
> Step 11: Trim Leading Space for each line
> Step 12: Trim Trailing Space after each line
> Step 13: Search for - (hyphen) Replace with _ (underscore)

> Step 14: Save file.

something like (untested)


import glob, codecs, re, os

regex =3D re.compile(r"Age: |Sex: |House No: ") # etc etc

for txt in glob.glob("/some/path/*.txt"):
=C2=A0 =C2=A0 with codecs.open(txt, encoding=3D"utf-8") as f:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 oldlines =3D f.readlines()
=C2=A0 =C2=A0 for i, line in enumerate(oldlines):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if "Contact's Name: " in line: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break
=C2=A0 =C2=A0 newlines =3D [regex.sub("", line).strip().replace(&= quot;-", "_") for line in oldlines[i:]
=C2=A0 =C2=A0 with codecs.open(txt + "_out.txt", "wb", = encoding=3D"utf-8") as w:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 w.write(os.linesep.join(newlines))




>
> Currently I have recorded a macro in Notepad++.
> I open each file, run macro and save file.
> As there are many files I was looking for a program to automate the pr= ocess.
>
> I posted the same query in Notepad++ forum. I got a reply that it can = be done by
> using Python script.
>
> Kindly do the needful.
>
> Thank you.
> khalidness
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



--001a11c238aee2edac0507740c83--