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


Groups > comp.lang.python > #51991

Sort lines in a plain text file alphanumerically

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'parameters': 0.04; 'subject:text': 0.05; 'attribute': 0.07; 'subject:file': 0.07; 'lines.': 0.09; 'newline': 0.09; 'sentence': 0.09; 'subject:skip:a 10': 0.09; 'assume': 0.14; 'sorting': 0.16; 'subject:Sort': 0.16; 'success:': 0.16; 'command': 0.22; '>>>': 0.22; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'file.': 0.24; 'sort': 0.25; 'second': 0.26; 'tried': 0.27; 'received:10.0.0': 0.31; '"",': 0.31; 'parameters.': 0.31; 'file': 0.32; 'text': 0.33; '(most': 0.33; 'plain': 0.33; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'received:10.0': 0.36; 'error.': 0.37; 'received:10': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'blank': 0.60; 'skip:o 30': 0.61; 'took': 0.61; 'matter': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'reverse': 0.68; 'from.': 0.93; 'wanting': 0.93
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 :content-type:content-transfer-encoding; bh=ppOZS2n2+Jmw89bHIDqymdLdbuM7bDONMjArwd6HEAY=; b=H4IKkiEiwz79LkECG0Q0mBK+AvUA2N5k5GSHitpe3yf9hV1zL+5kxVPunPVABWVz+t 1srPcOj7dkVtzHRL7sptvzFUr0Yp83LOgsemtibbF8RA8spCUu+wo5DGImZ4cXdGpnfN N/cAkcCa7YnVkfoCmkyVGKYEyFIwA1vbIdBxK8Tlvr1XRBkMqY/e9zc6TENHjUCXnrkr fesoLXCe3XLz2nSc4q8fx/jf+XgdjrsjtTRKcMQYzBMF/wZ008y0APupelZeIsjZpyMN +OdAT47HO32ReoHjAOY/vEJ/DmUS6RlOnsULeGRs9hdCWOSOgN5vBnPGVTIkHedVBKb7 NGjg==
X-Received by 10.60.132.230 with SMTP id ox6mr16521877oeb.66.1375754458624; Mon, 05 Aug 2013 19:00:58 -0700 (PDT)
Date Mon, 05 Aug 2013 22:00:55 -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 Sort lines in a plain text file alphanumerically
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.224.1375754462.1251.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375754462 news.xs4all.nl 15865 [2001:888:2000:d::a6]:50119
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51991

Show key headers only | View raw


I am wanting to sort a plain text file alphanumerically by the lines. I 
have tried this code, but I get an error. I assume this command does not 
accept newline characters.


 >>> file = open('/home/collier/pytest/sort.TXT', 'r').read()
 >>> print(file)
z
c
w
r
h
s
d


 >>> file.sort() #The first blank line above is from the file. I do not 
know where the second comes from.
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'sort'

I had the parameters (key=str.casefold, reverse=True), but I took those 
out to make sure the error was not with my parameters.

Specifically, I need something that will sort the lines. They may 
contain one word or one sentence with punctuation. I need to reverse the 
sorting ('z' before 'a'). The case does not matter ('a' = 'A').

I have also tried this without success:

 >>> file.sort(key=str.casefold, reverse=True)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'sort'


Mahalo,

DevynCJohnson@Gmail.com

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


Thread

Sort lines in a plain text file alphanumerically Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-08-05 22:00 -0400
  Re: Sort lines in a plain text file alphanumerically edu4madh@gmail.com - 2013-08-05 20:39 -0700

csiph-web