Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51991 > unrolled thread
| Started by | Devyn Collier Johnson <devyncjohnson@gmail.com> |
|---|---|
| First post | 2013-08-05 22:00 -0400 |
| Last post | 2013-08-05 20:39 -0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Devyn Collier Johnson <devyncjohnson@gmail.com> |
|---|---|
| Date | 2013-08-05 22:00 -0400 |
| Subject | Sort lines in a plain text file alphanumerically |
| Message-ID | <mailman.224.1375754462.1251.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | edu4madh@gmail.com |
|---|---|
| Date | 2013-08-05 20:39 -0700 |
| Message-ID | <a40fbb94-0fd8-4d80-9c98-939711655549@googlegroups.com> |
| In reply to | #51991 |
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 "<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
fileName = open('test.txt')
lines = fileName.readlines()
lines.sort()
print lines
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web