Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51994
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| 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; 'string.': 0.05; 'subject:text': 0.05; 'attribute': 0.07; 'method.': 0.07; 'subject:file': 0.07; 'collier': 0.09; 'lines.': 0.09; 'newline': 0.09; 'sentence': 0.09; 'subject:skip:a 10': 0.09; 'assume': 0.14; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'pythonic': 0.16; 'sorting': 0.16; 'subject:Sort': 0.16; 'success:': 0.16; 'wrote:': 0.18; 'command': 0.22; '>>>': 0.22; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'file.': 0.24; 'sort': 0.25; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'lines': 0.31; "skip:' 10": 0.31; '"",': 0.31; 'file:': 0.31; 'parameters.': 0.31; 'file': 0.32; 'text': 0.33; '(most': 0.33; 'plain': 0.33; 'something': 0.35; 'johnson': 0.35; 'but': 0.35; 'error.': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'days': 0.60; 'blank': 0.60; 'skip:o 30': 0.61; 'took': 0.61; 'matter': 0.61; 'first': 0.61; 'more': 0.64; 'header:Reply-To:1': 0.67; 'reverse': 0.68; 'reply-to:no real name:2**0': 0.71; "'with'": 0.84; 'actually,': 0.84; 'reply-to:addr:python.org': 0.84; 'from.': 0.93; 'wanting': 0.93 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=SJNpAhyQzXwA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=9Lbk4LHj9owA:10 a=osNPPAav4Kg4bRomqo8A:9 a=wPNLvfGTeEIA:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Tue, 06 Aug 2013 03:19:38 +0100 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Sort lines in a plain text file alphanumerically |
| References | <520058D7.10105@Gmail.com> |
| In-Reply-To | <520058D7.10105@Gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | python-list@python.org |
| 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.227.1375755575.1251.python-list@python.org> (permalink) |
| Lines | 55 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1375755575 news.xs4all.nl 15962 [2001:888:2000:d::a6]:58989 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:51994 |
Show key headers only | View raw
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 "<stdin>", line 1, in <module>
> 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 "<stdin>", line 1, in <module>
> 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()
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Sort lines in a plain text file alphanumerically MRAB <python@mrabarnett.plus.com> - 2013-08-06 03:19 +0100
csiph-web