Python: Unicode

Python has very good support for unicode, but half the time it's gets in the way. Java, by default, uses the Latin1 encoding (aka ISO-8859-1) which makes programming in languages like Portuguese very easy. Python's default, unfortunately, is 7 bit ASCII. If you use any accents you have to properly decode it for it to work without an error. A quick work around is to create a file called sitecustomize.py in your python lib directory and put in the following:
import sys

sys.setdefaultencoding('latin1')

This definitely makes coding with European accents a lot easier. Here's a good article on unicode and python. I've read that some people have had problems with sitecustomize and third party modules, but I believe that's only if you set it to utf8.
The other thing you need to do is put:
# -*- coding: latin1 -*-
near the top of your python program.
Neil Hodgson recomends testing your libraries with the -U switch for Python and by trying sys.setdefaultencoding('undefined').

Comments

Popular posts from this blog

Shortest Sudoku solver in Python

Downloading projecteuler.net