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


Groups > comp.lang.python > #92568

Re: Path, strings, and lines

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'lines,': 0.05; 'append': 0.07; 'rename': 0.07; 'lines:': 0.09; 'slow.': 0.09; 'cc:addr :python-list': 0.10; 'times,': 0.13; 'expecting': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inclined': 0.16; 'item:': 0.16; 'line)': 0.16; 'received:mail- ig0-x22a.google.com': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'fine,': 0.22; 'am,': 0.23; '2015': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'separate': 0.27; 'switch': 0.27; 'object,': 0.27; 'message- id:@mail.gmail.com': 0.28; 'actual': 0.29; '(although': 0.29; '13,': 0.29; 'once,': 0.29; "i'd": 0.31; 'open': 0.33; 'file': 0.34; 'received:google.com': 0.34; "i'll": 0.34; 'useful': 0.35; 'question,': 0.35; 'list': 0.35; "isn't": 0.35; 'but': 0.36; 'subject:: ': 0.37; 'item': 0.38; 'expect': 0.39; 'your': 0.60; 'more': 0.62; 'to,': 0.63; 'here': 0.66; 'chrisa': 0.84; 'find.': 0.84; 'open,': 0.84; 'to:none': 0.90; 'comment.': 0.91; 'lot,': 0.95
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:cc :content-type; bh=HPVyX/6Wbi3YD4drf7jeqz4Bya0weSYONgUTeFNfV1k=; b=oWGOSJMjGGWp15XNu7br6zqDQjyw7B5VMIccDOxWKtitT08Fgkiz1DoDx3MoBb+2/S wq2zky7AS7BiUHxPIQ+beao+/zu13f2yhGjeMjhGoo0epgfWpj11lDTm39RTur+McYZR A3BYl4mBqQ+EEp2Zk8uYrlEOY16KkQ1shlsgC4mNsRPjpgPOXtOuXaCUTaXhTWF5MHWS QlrByS1yYUVCGRoVn7nMFKFqcs2c437CSmJvIMIFIGcFcI2iRTsOhnSWeA9lMNen1XkP uBgitF3y6t3ViZc6k4n5VKIp8p/Ux93uPbdXx6QzcKJuFOfbXjlNlKjkEIxw15e1fPSV tI/A==
MIME-Version 1.0
X-Received by 10.107.131.200 with SMTP id n69mr13306400ioi.53.1434152848062; Fri, 12 Jun 2015 16:47:28 -0700 (PDT)
In-Reply-To <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com>
References <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com>
Date Sat, 13 Jun 2015 09:47:28 +1000
Subject Re: Path, strings, and lines
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.448.1434152856.13271.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434152856 news.xs4all.nl 2852 [2001:888:2000:d::a6]:53599
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92568

Show key headers only | View raw


On Sat, Jun 13, 2015 at 5:39 AM, Malik Rumi <malik.a.rumi@gmail.com> wrote:
> for line in lines:
>      for item in fileinput.input(s2):
>          if line in item:
>             with open(line + '_list', 'a+') as l:
>                 l.append(filename(), filelineno(), line)

Ian's already answered your actual question, but I'll make one
separate comment. What you have here will open, append to, and close,
the list file for every single line that you find. If you're expecting
to find zero or very few lines, then that's fine, but if you expect
you might find a lot, this will be extremely slow. Much more efficient
would be to open the file once, and write to it every time - just
switch around the nesting a bit:

with open(line + '_list', 'a+') as l:
     for line in lines:
         for item in fileinput.input(s2):
             if line in item:
                l.append(filename(), filelineno(), line)

(Although you may want to rename your open file object, here; "l"
isn't a very useful name at the best of times, so I'd be inclined to
call it "log".)

ChrisA

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


Thread

Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 12:39 -0700
  Re: Path, strings, and lines Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-12 14:30 -0600
    Re: Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 21:48 -0700
      Re: Path, strings, and lines MRAB <python@mrabarnett.plus.com> - 2015-06-13 19:25 +0100
        Struggling with os.path.join and fileinput (was 'Path, strings, and lines' Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-15 19:00 -0700
          Re: Struggling with os.path.join and fileinput (was 'Path, strings, and lines' Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-15 20:44 -0600
          Re: Struggling with os.path.join and fileinput (was 'Path, strings, and lines' MRAB <python@mrabarnett.plus.com> - 2015-06-16 03:51 +0100
  Re: Path, strings, and lines Chris Angelico <rosuav@gmail.com> - 2015-06-13 09:47 +1000
    Re: Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 21:50 -0700

csiph-web