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


Groups > comp.lang.python > #52024

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!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.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'parameters': 0.04; 'subject:text': 0.05; 'abuse': 0.07; 'attribute': 0.07; 'subject:file': 0.07; 'string': 0.09; 'collier': 0.09; 'exist,': 0.09; 'lines.': 0.09; 'newline': 0.09; 'says.': 0.09; 'sentence': 0.09; 'subject:skip:a 10': 0.09; 'assume': 0.14; '"with"': 0.16; 'doing,': 0.16; 'hint': 0.16; 'reason.': 0.16; 'sorting': 0.16; 'subject:Sort': 0.16; 'success:': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'first.': 0.19; 'command': 0.22; '>>>': 0.22; 'code,': 0.22; 'email addr:gmail.com&gt;': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; '&gt;&gt;&gt;': 0.24; "shouldn't": 0.24; 'file.': 0.24; 'sort': 0.25; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; '8bit%:3': 0.30; 'received:10.0.0': 0.31; '"",': 0.31; 'file:': 0.31; 'object.': 0.31; 'parameters.': 0.31; 'file': 0.32; 'probably': 0.32; 'text': 0.33; 'open': 0.33; '(most': 0.33; 'guess': 0.33; 'plain': 0.33; 'problem': 0.35; 'advice': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'convert': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'accessing': 0.36; 'skip:s 60': 0.36; 'thanks': 0.36; 'received:10.0': 0.36; 'error.': 0.37; 'list': 0.37; 'received:10': 0.37; 'skip:& 10': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'does': 0.39; 'bad': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; 'how': 0.40; 'blank': 0.60; 'is.': 0.60; 'then,': 0.60; 'august': 0.61; 'skip:o 30': 0.61; 'tips': 0.61; 'took': 0.61; 'matter': 0.61; "you're": 0.61; 'first': 0.61; 'close': 0.67; 'reverse': 0.68; 'useful.': 0.68; '#1:': 0.84; '#2:': 0.84; 'received:mail-ob0-x22d.google.com': 0.84; 'from.': 0.93; 'have.': 0.93; 'wanting': 0.93; '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; bh=iXPHvG9o6FBgONpyGEebqnVn/0uNAI1FKJuDrA+5ut8=; b=irETEeWEIjLrWzVzec1udAXX5KLi5hBv8jm0EVBHzYxXB7kV8+mtC661TtWmN+1+cp NBRVsJO5YC3Iya0Bp0dDKA+ndAs38vrhL3rE1xvGk8Sz9atJ3n01pQMWkoSL8fygjlaQ 84A+yqFM9t/6KHKJ5yhoI7RtJarl30m4Q4p1mfq+TcQ8ynVWJpkMvT74djtkVatukcz6 lsz0KS08flspjQYJRg98Szgir0nBWB1O3P60/BlMlb8cDUe0h3bkr2YLErT8ZxA3Y3Q/ nFGUmZpWigAZlAmUuWBbRCQiuEmeZVTvmEj2CfE/aymv1fcBQ2ucMY0tncYH+S8+D7p9 ozdw==
X-Received by 10.182.48.230 with SMTP id p6mr547492obn.1.1375785491942; Tue, 06 Aug 2013 03:38:11 -0700 (PDT)
Date Tue, 06 Aug 2013 06:38:08 -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>
In-Reply-To <CAN1F8qVU4wPHT=gMA1x6vpp0ccyxwPjiPBCLBvK3=T2S2dj9Sw@mail.gmail.com>
Content-Type multipart/alternative; boundary="------------040607090605090806090309"
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.239.1375785495.1251.python-list@python.org> (permalink)
Lines 233
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375785495 news.xs4all.nl 15984 [2001:888:2000:d::a6]:53125
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52024

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 08/05/2013 11:12 PM, Joshua Landau wrote:
> On 6 August 2013 03:00, Devyn Collier Johnson <devyncjohnson@gmail.com 
> <mailto:devyncjohnson@gmail.com>> wrote:
>
>     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.
>
>
> HINT #1: Don't assume that without a reason. It's wrong.
>
>     >>> file = open('/home/collier/pytest/sort.TXT', 'r').read()
>
>
> HINT #2: Don't lie. "file" is not a file so you probably shouldn't 
> call it one. It's the contents of a file object.
>
>     >>> 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'
>
>
> HINT #3: *Read*. What does it say?
>
>     AttributeError: 'str' object has no attribute 'sort'
>
> Probably your problem, then, is that a 'str' object has no attribute 
> 'sort'. That's what it says.
>
> "file" is a "'str' object". You are accessing the 'sort' attribute 
> which it doesn't have.
>
>     I had the parameters (key=str.casefold, reverse=True), but I took
>     those out to make sure the error was not with my parameters.
>
>
> HINT #4: Don't just guess what the problem is. The answer is in the error.
>
>     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'
>
>
>
>
> So you want to sort your string by lines. Rather than trying to abuse 
> a .sort attribute that patently doesn't exist, just use sorted OR 
> convert to a list first.
>
>     sorted(open('/home/collier/pytest/sort.TXT'), key=str.casefold, 
> reverse=True)
>
> Because it's bad to open files without a with unless you know what 
> you're doing, use a with:
>
>     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?

Mahalo,

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 06:38 -0400

csiph-web