Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52023
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <devyncjohnson@gmail.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; 'mrab': 0.05; '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; 'command,': 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; 'received:10.0.0': 0.31; "skip:' 10": 0.31; '"",': 0.31; 'file:': 0.31; 'parameters.': 0.31; 'file': 0.32; 'thanks!': 0.32; 'run': 0.32; 'text': 0.33; '(most': 0.33; 'plain': 0.33; 'something': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'received:10.0': 0.36; 'error.': 0.37; 'received:10': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'days': 0.60; 'even': 0.60; 'blank': 0.60; 'skip:o 30': 0.61; 'took': 0.61; 'matter': 0.61; 'first': 0.61; 'more': 0.64; 'reverse': 0.68; "'with'": 0.84; 'actually,': 0.84; 'from.': 0.93; 'wanting': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=8OuJvRMa79+8SQIbUJWeDmjJxVIyRRpjQJ0OMeTm1h8=; b=e6bcVLxz/Pco+/Bj0Z7KkeLMxkUwml01bLLX9FfBAISMGJtnNlb+aSC+EeuFv5zBf4 LbPRoP+XEoeiVi6hNsR391fOQJSxafZ3IH+94uHySV/FRlAr5OmTqkuMKG/yVHAj/nzF 2yrQr6y8OUlJZNUq+HqbXxezfyVVHi6gr/oElsaBg9/468KDONSshdr5TekBvfnbmb5w /U+CntXE/oix8fMhTlUeI6okepDF3zdT7gHRM9D63XlHWr3jVtt3QXsGYmb1hPo29eDf gsTpzQYIkXk1jrxSbzBo9y1dp3IBpVXWdMx5HK9cnmDhtjLnzY40j9fyuSVaNFPnu0Oi Dwfw== |
| X-Received | by 10.182.81.41 with SMTP id w9mr510338obx.18.1375785349784; Tue, 06 Aug 2013 03:35:49 -0700 (PDT) |
| Date | Tue, 06 Aug 2013 06:35:46 -0400 |
| From | Devyn Collier Johnson <devyncjohnson@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Sort lines in a plain text file alphanumerically |
| References | <520058D7.10105@Gmail.com> <52005D3A.2020602@mrabarnett.plus.com> |
| In-Reply-To | <52005D3A.2020602@mrabarnett.plus.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 |
| 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.238.1375785358.1251.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1375785358 news.xs4all.nl 16002 [2001:888:2000:d::a6]:50662 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:52023 |
Show key headers only | View raw
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 "<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()
>
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Sort lines in a plain text file alphanumerically Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-08-06 06:35 -0400
csiph-web