Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!border2.nntp.ams2.giganews.com!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*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; 'cc:addr:python-list': 0.11; 'assume': 0.14; 'doing,': 0.16; 'hint': 0.16; 'reason.': 0.16; 'sorting': 0.16; 'subject:Sort': 0.16; 'success:': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'trying': 0.19; 'first.': 0.19; 'command': 0.22; '>>>': 0.22; 'code,': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; '>>>': 0.24; "shouldn't": 0.24; 'file.': 0.24; 'cc:2**0': 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; 'message-id:@mail.gmail.com': 0.30; '"",': 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; '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; 'error.': 0.37; 'list': 0.37; 'skip:& 10': 0.38; 'files': 0.38; 'rather': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'does': 0.39; 'bad': 0.39; 'sure': 0.39; '8bit%:6': 0.40; 'blank': 0.60; 'is.': 0.60; 'then,': 0.60; 'august': 0.61; 'skip:o 30': 0.61; 'took': 0.61; 'matter': 0.61; "you're": 0.61; 'first': 0.61; 'to:addr:gmail.com': 0.65; 'reverse': 0.68; '#1:': 0.84; '#2:': 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=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=vDxOfK9pjlIqM8B4x4jzG7GyNCwiyQlCWxKrXmbFeHw=; b=noxngOENpVE00KdcLqg62bi5L+OBKos4xZAi1mvifq1vEQB0ZLQCe64fDG8HkRlIw8 7QP45PZ7uVV3OBM1uN0QpIiXRbTWZtCNBQCd0XMKy2NgDUQr4Bq5zqP3lWpZ4YV5sZ7R 9foAAPwDrAiMKhDlp7GyET0MTeZkue8zrQgXPydd7NuaNTAXOoZ3WJx0QLWZmsKHneOn dxE1lZtmrLZnSvDHI4gmtsD5JRmswjP+FbwLYMtGcdBO8GNIOWVvMl2SFcoPdUOG0DQ9 vee0JKAGfOkKsjz55UOpf/H42BSJwA5bDyl/IUWtHG3Yc656bgIgmgCR/gCbFOB/jfj0 fbfg== X-Received: by 10.152.8.228 with SMTP id u4mr9866873laa.79.1375758795777; Mon, 05 Aug 2013 20:13:15 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: <520058D7.10105@Gmail.com> References: <520058D7.10105@Gmail.com> From: Joshua Landau Date: Tue, 6 Aug 2013 04:12:35 +0100 X-Google-Sender-Auth: vgpYEGkn2F1VfuKN6T-8Dv3pLCw Subject: Re: Sort lines in a plain text file alphanumerically To: Devyn Collier Johnson Content-Type: multipart/alternative; boundary=089e0158ba9e51ac4004e33ece38 Cc: Python Mailing List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 174 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375758799 news.xs4all.nl 15899 [2001:888:2000:d::a6]:37078 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51995 --089e0158ba9e51ac4004e33ece38 Content-Type: text/plain; charset=UTF-8 On 6 August 2013 03:00, Devyn Collier Johnson 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 "", line 1, in > 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 "", line 1, in > 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) --089e0158ba9e51ac4004e33ece38 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 6 August 2013 03:00, Devyn Collier Johnson <devy= ncjohnson@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 comma= nd does not accept newline characters.

HINT #1: Don't assume that without a r= eason. It's wrong.
=C2=A0
>>> file =3D 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 o= ne. It's the contents of a file object.
=C2=A0
>>> 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):
=C2=A0 File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'sort'

HINT #3: *Read*. What does it say?=C2=A0

=C2=A0 =C2=A0 AttributeError: 'str' object has= no attribute 'sort'

Probably your problem, then, is that a 'str&#= 39; object has no attribute 'sort'. That's what it says.
<= div>
"file" is a "'str' object". = You are accessing the 'sort' attribute which it doesn't have.

I had the parameters (key=3Dstr.casefold, reverse=3DTrue), 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 ans= wer is in the error.
=C2=A0
Specifically, I need something that will sort the lines. They may contain o= ne word or one sentence with punctuation. I need to reverse the sorting (&#= 39;z' before 'a'). The case does not matter ('a' =3D &#= 39;A').

I have also tried this without success:

>>> file.sort(key=3Dstr.casefold, reverse=3DTrue)
Traceback (most recent call last):
=C2=A0 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 th= at patently doesn't exist, just use sorted OR convert to a list first.<= /div>

=C2=A0 =C2=A0 sorted(open('/home/collier/pytest/= sort.TXT'),=C2=A0key=3Dstr.casefold, reverse=3DTrue)

=
Because it's bad to open files without a with unless you kno= w what you're doing, use a with:

=C2=A0 =C2=A0 with open('/home/collier/pytest/sort.TXT') as file:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 sorted(file= ,=C2=A0key=3Dstr.casefold, reverse=3DTrue)
--089e0158ba9e51ac4004e33ece38--