Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51995 > unrolled thread
| Started by | Joshua Landau <joshua@landau.ws> |
|---|---|
| First post | 2013-08-06 04:12 +0100 |
| Last post | 2013-08-06 06:40 -0400 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Sort lines in a plain text file alphanumerically Joshua Landau <joshua@landau.ws> - 2013-08-06 04:12 +0100
Re: Sort lines in a plain text file alphanumerically alex23 <wuwei23@gmail.com> - 2013-08-06 13:49 +1000
Re: Sort lines in a plain text file alphanumerically alex23 <wuwei23@gmail.com> - 2013-08-06 13:51 +1000
Re: Sort lines in a plain text file alphanumerically alex23 <wuwei23@gmail.com> - 2013-08-06 14:33 +1000
Re: Sort lines in a plain text file alphanumerically Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-08-06 06:40 -0400
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2013-08-06 04:12 +0100 |
| Subject | Re: Sort lines in a plain text file alphanumerically |
| Message-ID | <mailman.228.1375758799.1251.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
On 6 August 2013 03:00, Devyn Collier Johnson <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)
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-08-06 13:49 +1000 |
| Message-ID | <ktprbs$lpf$1@dont-email.me> |
| In reply to | #51995 |
On 6/08/2013 1:12 PM, Joshua Landau wrote:
> 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)
Shouldn't that be:
with open('/home/collier/pytest/__sort.TXT') as file:
data = file.readlines()
sorted(data, key=str.casefold, reverse=True)
I'm tempted to say "HINT #5: don't provide a solution without testing it
first" but that would be pretty obnoxious.
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-08-06 13:51 +1000 |
| Message-ID | <ktprfh$lpf$2@dont-email.me> |
| In reply to | #51997 |
On 6/08/2013 1:49 PM, alex23 wrote:
> Shouldn't that be:
>
> with open('/home/collier/pytest/__sort.TXT') as file:
> data = file.readlines()
> sorted(data, key=str.casefold, reverse=True)
>
> I'm tempted to say "HINT #5: don't provide a solution without testing it
> first" but that would be pretty obnoxious.
Even more so when I got it wrong myself :)
data = sorted(file.readlines(), key=str.casefold, reverse=True)
I can never remember which one sorts in place and which doesn't.
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-08-06 14:33 +1000 |
| Message-ID | <ktpttt$vif$1@dont-email.me> |
| In reply to | #51997 |
On 6/08/2013 1:49 PM, alex23 wrote:
> On 6/08/2013 1:12 PM, Joshua Landau wrote:
>> 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)
>
> Shouldn't that be:
>
> with open('/home/collier/pytest/__sort.TXT') as file:
> data = file.readlines()
> sorted(data, key=str.casefold, reverse=True)
Hmm, I take that back entirely. Your version does work. Weirdly, I tried
yours under both 2.7 & 3.2 without it working at all, but a subsequent
attempt did. Sorry for the noise.
[toc] | [prev] | [next] | [standalone]
| From | Devyn Collier Johnson <devyncjohnson@gmail.com> |
|---|---|
| Date | 2013-08-06 06:40 -0400 |
| Message-ID | <mailman.240.1375785609.1251.python-list@python.org> |
| In reply to | #51997 |
On 08/05/2013 11:49 PM, alex23 wrote:
> On 6/08/2013 1:12 PM, Joshua Landau wrote:
>> 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)
>
> Shouldn't that be:
>
> with open('/home/collier/pytest/__sort.TXT') as file:
> data = file.readlines()
> sorted(data, key=str.casefold, reverse=True)
>
> I'm tempted to say "HINT #5: don't provide a solution without testing
> it first" but that would be pretty obnoxious.
I tried Joshua's suggestion in Python3.3 and it worked. What version of
Python are you using?
DCJ
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web