Friday, September 21, 2012

More Unicode strangeness in Python 2.x

$ python -c "print u'Hey there\u2013t'"
Hey there–t
$ python -c "print u'Hey there\u2013t'" > `tempfile`
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 9: ordinal not in range(128)
The solution is:
PYTHONIOENCODING="utf_8" python -c "print u'Hey there\u2013t'" > `tempfile`

No comments:

Post a Comment