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


Groups > comp.lang.python > #52037

Re: Sort lines in a plain text file alphanumerically

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <devyncjohnson@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*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; 'subject:Sort': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'file.': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; 'code': 0.31; 'received:10.0.0': 0.31; 'file:': 0.31; 'file': 0.32; 'thanks!': 0.32; 'advice': 0.35; 'johnson': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'object,': 0.36; 'thanks': 0.36; 'received:10.0': 0.36; 'received:10': 0.37; '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=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=TugmNOsNVeJhNAkg855AEH4UideDJtKKgk/8SyOTBSc=; b=SUWnzj5LlzOpRjWsigt+auVhlOKCeEiSP9Ax+4Xh7l53W6lO1ijYSdlmj3c/XKpG2x xk6M91UEvVKbMlRHVbNAj+tgXi70j3eySugUvsDCx8AEZ3GFcXMKdwM6MCb33hKLnyFN MUaS/h44c039BcrcMA+c7C0xq79NfKkUaQqQP33yWtmATejyHdwmF3As2bQFETdoPNRy IUEQG9kAzuddviIdTN62i9wIMT/A80e7AqhDxoLgaJSokPs+3UAI5SB4uqkJmWD3afyi a5mBm05XD3hv3JREQ8oidrk2aP6JVcHWgboOX6ecOShhAt9mvI9KKdFKG+eoTit/svAS XlmQ==
X-Received by 10.52.249.52 with SMTP id yr20mr258306vdc.35.1375792188903; Tue, 06 Aug 2013 05:29:48 -0700 (PDT)
Date Tue, 06 Aug 2013 08:29:46 -0400
From Devyn Collier Johnson <devyncjohnson@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130803 Thunderbird/17.0.8
MIME-Version 1.0
To Python Mailing List <python-list@python.org>
Subject Re: Sort lines in a plain text file alphanumerically
References <520058D7.10105@Gmail.com> <CAN1F8qVU4wPHT=gMA1x6vpp0ccyxwPjiPBCLBvK3=T2S2dj9Sw@mail.gmail.com> <5200D210.9030003@Gmail.com> <CAPTjJmqMLYXNGEnEzi0u=g6uu0apz0BabOGgftAYdDN0XcQF4w@mail.gmail.com>
In-Reply-To <CAPTjJmqMLYXNGEnEzi0u=g6uu0apz0BabOGgftAYdDN0XcQF4w@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.249.1375792198.1251.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375792198 news.xs4all.nl 15945 [2001:888:2000:d::a6]:48992
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52037

Show key headers only | View raw


On 08/06/2013 06:52 AM, Chris Angelico wrote:
> 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

Thanks! Now I see why using "with" is a better way to write the code.

DCJ

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


Thread

Re: Sort lines in a plain text file alphanumerically Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-08-06 08:29 -0400

csiph-web