Python 2 Unicode Problem
The following Python error is one of the most annoying one’s I’ve ever encountered:
Essentially, you can’t write unicode characters as string unless you’ve converted the text to a string first before printing it. A detailed explanation can be found in Kumar McMillan’s wonderful talk titled ‘Unicode in Python, Completely Demystified’. To summarize, McMillan offers three useful yet simple rules:
- Decode early (after reading from a file)
- Unicode everywhere
- Encode late (for writing to a file or printing to the screen)
As recommended by McMillan, one could create two utility functions:
which can be used like this:
Happy Unicoding!