Case-insensitive sorting of strings (Python newbie)

Chris Angelico rosuav at gmail.com
Fri Jan 23 18:58:54 CET 2015


On Sat, Jan 24, 2015 at 4:53 AM, Peter Otten <__peter__ at web.de> wrote:
> Now the same with unicode. To read text with a specific encoding use either
> codecs.open() or io.open() instead of the built-in (replace utf-8 with your
> actual encoding):
>
>>>> import io
>>>> for line in io.open("tmp.txt", encoding="utf-8"):
> ...     line = line.strip()
> ...     print line, line.lower()

In Python 3, the built-in open() function sports a fancy encoding=
parameter like that.

for line in open("tmp.txt", encoding="utf-8"):

If you can, I would recommend using Python 3 for all this kind of
thing. The difference may not be huge, but there are all sorts of
little differences here and there that mean that Unicode support is
generally better; most of it stems from the fact that the default
quoted string literal is a Unicode string rather than a byte string,
which means that basically every function ever written for Py3 has
been written to be Unicode-compatible.

ChrisA



More information about the Python-list mailing list