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


Groups > comp.lang.python > #52030

Re: Sort lines in a plain text file alphanumerically

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.019
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'subject:text': 0.05; 'subject:file': 0.07; 'collier': 0.09; 'subject:skip:a 10': 0.09; '"with"': 0.16; 'block.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:Sort': 0.16; 'wrote:': 0.18; 'aug': 0.22; 'file.': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'file:': 0.31; 'file': 0.32; 'advice': 0.35; 'johnson': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'object,': 0.36; 'thanks': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:o 30': 0.61; 'tips': 0.61; 'close': 0.67; 'useful.': 0.68; '2013': 0.98
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 :content-type; bh=YPF/lecNmtsEMt4BafEo2skfsA1qW8PIVEzsPV0HmIw=; b=wvZP18FXKphSw25eE/7ou3jn3vr1ZUisxFyvwuLGdssCdcSEkma35fhm4v58LahISK BWDMwVAuqQNiJ0nOpqcwlCC2i65YQZpt+M7UxWqPKfOTntEZor4FpLAE4ZAI6nlzfmtz CiwQb6ciDGA/baPc4AGegIcMp27Hah0ptW6MwpHmhSGk3pD3MYAJ3WZtCU4dy8IKUu8c nGskOr0QTEzrvRn3qLw4YUO1Aso9TOOd42brpwfCnCvIyviJvY03JEt5RfV1lbxZl5Sf a75dKyRM+5NHHsgXm3FjBea1g4clFujA5J/SLlN3vl2MKl92t8eht+6HyRbiTgH9LwUv yRWw==
MIME-Version 1.0
X-Received by 10.220.182.193 with SMTP id cd1mr201873vcb.32.1375786334517; Tue, 06 Aug 2013 03:52:14 -0700 (PDT)
In-Reply-To <5200D210.9030003@Gmail.com>
References <520058D7.10105@Gmail.com> <CAN1F8qVU4wPHT=gMA1x6vpp0ccyxwPjiPBCLBvK3=T2S2dj9Sw@mail.gmail.com> <5200D210.9030003@Gmail.com>
Date Tue, 6 Aug 2013 11:52:14 +0100
Subject Re: Sort lines in a plain text file alphanumerically
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.242.1375786342.1251.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375786342 news.xs4all.nl 15888 [2001:888:2000:d::a6]:38847
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52030

Show key headers only | View raw


On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
<devyncjohnson@gmail.com> wrote:
>     with open('/home/collier/pytest/sort.TXT') as file:
>         sorted(file, key=str.casefold, reverse=True)
>
>
> Thanks for the advice Joshua. I find these tips very useful. However, how
> would I close the files, or would they close after the "with" construct is
> complete?


That's the whole point of 'with'. It calls open(), then calls
__enter__, and it guarantees to call __exit__ before executing any
code following the with block. With a file object, __exit__ will close
the file.

ChrisA

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


Thread

Re: Sort lines in a plain text file alphanumerically Chris Angelico <rosuav@gmail.com> - 2013-08-06 11:52 +0100

csiph-web