In my Python script running at the command line on Ubuntu, it’s selecting UTF8-encoded content from a MySQL database.
Then, I want to print the string to the console.
The displayed strings have an encoding problem, as they don’t show the accented characters correctly. How do I fix this?
Preferably, the script would make the decision, rather than setting a system environment, to ensure easy running on other systems.
It is very strongly recommended that you not use “?” as a replacement char. Just set your output encoding to UTF-8 and be done with it.
Alternately, set your
PYTHONIOENCODINGenvariable to utf8 so that python stops guessing about the output encoding.Either approach is infinitely much better than manually encoding, which is stupid.
If you refuse to upgrade to Python3, I also recommend
to banish all that stupid
u'...'stuff.Lately I’ve starting all my Python progams like this: