[Newbie]Trouble with string method

Sean Berry sean_berry at cox.net
Thu Mar 25 16:02:52 EST 2004


In order to do what you are trying here...
you would have to do something like the following.

L2 = []
for item in L:
    L2.append(string.lower(item))

then do your sort
L2.sort

This way, you are applying the lower to a string, and not the list.
If you do not need to change the case of all characters to lower, then
you should use my suggestion from my previous post.

If you do need to lower case all characters... then try what I have above.


calpolynate <nathanjohns77 at hotmail.com> wrote in message
news:96fd272b.0403251442.49047ba9 at posting.google.com...
> I'm attempting to open a file, read the lines, sort them, change any
> uppercase character to lowercase, and output to another file:
>
> import string
>
> unsorted = open('unsorted', 'r')
> L = unsorted.readlines()
> L.sort()
> L = L.lower()
> sorted = open('sorted', 'w')
> sorted.writelines(L)
> unsorted.close()
> sorted.close()
>
> when trying to run, I get this traceback:
>
> Traceback (most recent call last):
>   File "sort_list.py", line 6, in ?
>     L = L.lower(L)
> AttributeError: 'list' object has no attribute 'lower'
>
> Do I need to somehow convert the list into strings?
>
> Thanks for any help!





More information about the Python-list mailing list