Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Chris Angelico
On Tue, Aug 6, 2013 at 12:44 PM, Joshua Landau  wrote:
> On 6 August 2013 11:52, Chris Angelico  wrote:
>> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
>>  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(),
>
> To be pedantic, it does not. open is called by the time the with gets 
> involved.
>

The entire statement does. Yes, it's not 'with' that does that, but it
is called. Anyway, that's detaily stuff :)

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Devyn Collier Johnson


On 08/06/2013 06:52 AM, Chris Angelico wrote:

On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Joshua Landau
On 6 August 2013 11:52, Chris Angelico  wrote:
> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
>  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(),

To be pedantic, it does not. open is called by the time the with gets involved.

> 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.


To make it more obvious for Devyn¹, with is used any time you need a
simple guarantee of when things are "closed", for any particular
meaning. One popular usage, just as a taster, is with changing
directories so that the original directory is restored upon completion
of the sub-tasks.

¹ And so it doesn't look like I wrote a whole post to be pedantic
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Chris Angelico
On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Devyn Collier Johnson


On 08/05/2013 11:12 PM, Joshua Landau wrote:
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)


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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Devyn Collier Johnson


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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Devyn Collier Johnson


On 08/05/2013 10:19 PM, MRAB wrote:

On 06/08/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.


  >>> file = open('/home/collier/pytest/sort.TXT', 'r').read()


That returns the file as a single string.


  >>> 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'


Strings don't have a sort method.


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 "", line 1, in 
AttributeError: 'str' object has no attribute 'sort'


Try this:

lines = open('/home/collier/pytest/sort.TXT', 'r').readlines()
lines.sort()


Actually, a more Pythonic way these days is to use the 'with' statement:

with open('/home/collier/pytest/sort.TXT') as file:
lines = file.readlines()

lines.sort()



Thanks! That works well. After I run your command, I run

print(''.join(lines))

to get the sorted output. Even the parameters work without issues.

lines.sort(reverse=True, key=str.casefold)


Mahalo,

DCJ
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-06 Thread Devyn Collier Johnson


On 08/05/2013 10:19 PM, MRAB wrote:

On 06/08/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.


  >>> file = open('/home/collier/pytest/sort.TXT', 'r').read()


That returns the file as a single string.


  >>> 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'


Strings don't have a sort method.


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 "", line 1, in 
AttributeError: 'str' object has no attribute 'sort'


Try this:

lines = open('/home/collier/pytest/sort.TXT', 'r').readlines()
lines.sort()


Actually, a more Pythonic way these days is to use the 'with' statement:

with open('/home/collier/pytest/sort.TXT') as file:
lines = file.readlines()

lines.sort()



Thanks! That works well. After I run your command, I run

print(''.join(lines))

to get the sorted output. Even the parameters work without issues.

lines.sort(reverse=True, key=str.casefold)


Mahalo,

DCJ
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-05 Thread alex23

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.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-05 Thread alex23

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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-05 Thread alex23

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.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-05 Thread edu4madh
On Monday, August 5, 2013 10:00:55 PM UTC-4, 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.
> 
> 
> 
> 
> 
>  >>> 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 "", line 1, in 
> 
> 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 "", line 1, in 
> 
> AttributeError: 'str' object has no attribute 'sort'
> 
> 
> 
> 
> 
> Mahalo,
> 
> 
> 
> devyncjohn...@gmail.com

fileName = open('test.txt')
lines = fileName.readlines()
lines.sort()
print lines
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-05 Thread Joshua Landau
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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort lines in a plain text file alphanumerically

2013-08-05 Thread MRAB

On 06/08/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.


  >>> file = open('/home/collier/pytest/sort.TXT', 'r').read()


That returns the file as a single string.


  >>> 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'


Strings don't have a sort method.


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 "", line 1, in 
AttributeError: 'str' object has no attribute 'sort'


Try this:

lines = open('/home/collier/pytest/sort.TXT', 'r').readlines()
lines.sort()


Actually, a more Pythonic way these days is to use the 'with' statement:

with open('/home/collier/pytest/sort.TXT') as file:
lines = file.readlines()

lines.sort()

--
http://mail.python.org/mailman/listinfo/python-list


Sort lines in a plain text file alphanumerically

2013-08-05 Thread Devyn Collier Johnson
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 "", line 1, in 
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 "", line 1, in 
AttributeError: 'str' object has no attribute 'sort'


Mahalo,

devyncjohn...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list