Case-insensitive sorting of strings (Python newbie)

Michael Ströder michael at stroeder.com
Fri Jan 23 12:10:03 EST 2015


John Sampson wrote:
> I notice that the string method 'lower' seems to convert some strings (input
> from a text file) to Unicode but not others.
> This messes up sorting if it is used on arguments of 'sorted' since Unicode
> strings come before ordinary ones.

I doubt that. Can you provide a short example?

> Is there a better way of case-insensitive sorting of strings in a list? Is it
> necessary to convert strings read from a plaintext file
> to Unicode? If so, how? This is Python 2.7.8.

Well, if you have non-ASCII chars for many Unicode characters str.lower()
won't give reasonable results. So binary strings containing an encoding of
Unicode character entities should be decoded to Unicode strings first.

Ciao, Michael.




More information about the Python-list mailing list