2018-08-19T10:21:07
Python 2 Unicode
Create unicode string "a".
>>> a = u'\u2019'
>>> a
u'\u2019'
Convert to ASCII string (ASCII is default for Python 2 str()
).
>>> str(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 0: ordinal not in range(128)
Encode the unicode string to UTF-8 (overriding the default of ASCII).
>>> str(a.encode('utf-8'))
'\xe2\x80\x99'
>>> print str(a.encode('utf-8'))
’
For a greater understanding, see Ned Batchelder's great "Pragmatic Unicode, or, How do I stop the pain?" (2012) video.