What's the best/neatest way to get Unicode data from a database into a grid cell?

Chris Angelico rosuav at gmail.com
Sun Feb 7 06:56:07 EST 2016


On Sun, Feb 7, 2016 at 10:19 PM,  <cl at isbd.net> wrote:
> My system (xubuntu 15.10) is all UTF8 so accented characters are
> handled by the display, in terminals, etc. correctly.  I'm currently
> using python 2.7 for this but would be quite happy to move to 3.4 if
> this handles UTF8 better (I seem to remember it does maybe).

As a general rule, yes, Python 3 handles Unicode somewhat better than
Python 2 does. The main reason for this is that the native string type
is now a Unicode string, instead of the native string type being a
byte string; so "abcd" is not a string of bytes that happen to be
encoded as UTF-8, it is actually a string of Unicode characters. For
the most part, you'll be able to ignore character encodings and such,
and just work with text.

So the question then becomes: Under Python 3, can you use regular
string objects for both the things you're working with? I can confirm
that the inbuilt sqlite3 module works just fine with Unicode text; so
all you need to do is try out your GUI code under Python 3. I've no
idea how good wx support in Py3 is, so you might find you need to
switch GUI toolkits to get everything working; but whether it's with
wxWidgets, GTK, QT, or some other library, you should be able to put
something together under Python 3 that "just works" as regards
Unicode. Caveat: I haven't done any serious GUI programming using
Python.

(Note that "just works" can actually be a lot of hassle if you truly
want to support *all* of Unicode. I've seen programs that don't behave
correctly in the presence of combining characters, or right-to-left
text, or zero-width characters, or non-BMP characters; but you can get
far better support for the same amount of effort if you use Py3 and
Unicode than if you try to do things manually under Py2.)

ChrisA


More information about the Python-list mailing list