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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <fomcl@yahoo.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.079
X-Spam-Evidence '*H*': 0.85; '*S*': 0.00; 'subject:Python': 0.06; 'line:': 0.09; 'subject:files': 0.09; 'subject:script': 0.09; 'cc:addr:python-list': 0.11; '"_")': 0.16; 'cet': 0.16; 'newlines': 0.16; 'script,': 0.16; 'subject:txt': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'to:2**1': 0.27; 'to:no real name:2**1': 0.29; 'skip:g 30': 0.30; 'subject:that': 0.31; 'txt': 0.31; 'worked': 0.33; 'etc': 0.35; 'skip:> 10': 0.36; 'charset:us- ascii': 0.36; 'skip:- 20': 0.37; 'nov': 0.38; 'skip:[ 10': 0.38; 'received:98.137': 0.60; 'break': 0.61; "you're": 0.61; 'to:addr:gmail.com': 0.65; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'received:98.136': 0.69; 'million': 0.74; 'no:': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84; 'subject:find': 0.84; 'syed': 0.84; '|house': 0.84
X-Yahoo-Newman-Property ymail-3
X-Yahoo-Newman-Id 741299.10369.bm@omp1048.mail.gq1.yahoo.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1415573903; bh=+8IEfoyHv9Abq86k8NMHmDs5HBq6TLPYClxjiYeTu3A=; h=Message-ID:Date:From:Subject:To:Cc:MIME-Version:Content-Type; b=FY1dsOxBLLMbCzTSn/vNyl4U1wSQMkP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vbXBpbGUociJBZ2U6IHxTZXg6IHxIb3VzZSBObzogICIpICMgZXRjIGV0Yw0KPg0KPmZvciB0eHQgaW4BMAEBAQE-
X-Mailer YahooMailWebService/0.8.203.733
Date Sun, 9 Nov 2014 14:58:23 -0800
From Albert-Jan Roskam <fomcl@yahoo.com>
Subject Re: Python script that does batch find and replace in txt files
To khalidness@gmail.com, tameemsmiles@gmail.com
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15612.1415574059.18130.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415574059 news.xs4all.nl 2862 [2001:888:2000:d::a6]:59477
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80446

Show key headers only | 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