Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.078 X-Spam-Evidence: '*H*': 0.84; '*S*': 0.00; 'subject:Python': 0.06; 'automate': 0.07; 'is"': 0.07; 'cursor': 0.09; 'line:': 0.09; 'rows': 0.09; 'subject:files': 0.09; 'subject:script': 0.09; 'trailing': 0.09; 'python': 0.11; 'posted': 0.15; '"_")': 0.16; '.txt': 0.16; 'newlines': 0.16; 'recorded': 0.16; 'subject:txt': 0.16; 'the"': 0.16; 'files.': 0.16; 'folder': 0.16; 'file,': 0.19; 'select': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'replace': 0.24; 'script.': 0.24; 'file.': 0.24; 'script': 0.25; 'query': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'characters': 0.30; 'skip:g 30': 0.30; 'url:mailman': 0.30; 'lines': 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; 'url:python': 0.33; '-----': 0.33; 'something': 0.35; 'etc': 0.35; 'there': 0.35; 'done': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.37; 'step': 0.37; 'thank': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'does': 0.39; 'subject:': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'space': 0.40; 'url:mail': 0.40; 'remove': 0.60; 'blank': 0.60; 'received:98.137': 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; 'header:Reply-To:1': 0.67; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'skip:w 30': 0.69; 'containing': 0.69; 'received:98.136': 0.69; '"contact': 0.84; '10:': 0.84; '11:': 0.84; '12:': 0.84; '13:': 0.84; '14:': 0.84; 'no:': 0.84; 'received:bullet.mail.gq1.yahoo.com': 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=yahoo.com; s=s2048; t=1415564666; bh=46K/75EqC8hikdRWs+J2pr73oowZ6fHjXjKvVzh38ZI=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=J1jd21+PrHPnIZ5SqjLmkJcQjqZrp0AWnlmm3rbjeDBTFHTMFrQ/4z3b4vekClKXtxaTszRyMI0Bq1nN5u8gDI7xT6ba92fuUpZv+P/t2mXvMjkWoGaTqOOND81K3nAFUZnkg366Or53k8nQHdNEU3tXWCTC0Cua2AAQhCp6+l3Bxr1U801KtHhf5rwGnbYaDaZwW2XKWdBJ3+a2/RiRFduNxfAhfhk1+82Zmk49I4QnBa+O+ybbe1oiHvvUX54UId69MpoH/B16fSPuUciWQMG6/82rKlMaPekbNb9xiCdXTD9ww+D4RtHfteLHs3a8VQqHpuAGjhp7sD2VgmQY9A== DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=yahoo.com; b=jMxjRfeUjoR84Mb9NBjgOEL9lTvsUDXpovKKgNGsPUmnxz5AZvckG/GNvAbMTthd9WH/q0A8pKfYLlz5RQ2MZWqa+Wrll/IO+pEB7jLKbcNf8lIwIxU+qLR4EQVOTzyUDeZBsDvGpkuq17KrIJMxyACVxB/nxS3DhTPVaz/fSErPLdika0AC37ySGGvVsFdZMgICE8XUgA11TIwm1QJa89VGbK2zYm2ZHleJ65ZlwfTxOE+qj6mmxLNbNsjxLs0rH8oUQarwJRA5n03loOwZ8Y5HEetTeDJdEG0HSbQ9io+OQNZ0JksHTjJa7/YHt6XRnTxjVXXU8b6CQ+VKzAuo8Q==; X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 630994.62265.bm@omp1041.mail.gq1.yahoo.com Date: Sun, 9 Nov 2014 20:23:20 +0000 (UTC) From: Albert-Jan Roskam To: Syed Khalid , "python-list@python.org" In-Reply-To: References: Subject: Re: Python script that does batch find and replace in txt files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Albert-Jan Roskam 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415564839 news.xs4all.nl 2954 [2001:888:2000:d::a6]:37504 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80440 ----- 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 >