Python doctests and unicode

2019-02-18 10:00发布

问题:

I have a code base that runs unchanged in Python 2.7 and 3.2+. But the doctests in the documentation rst files are giving me a headache. When I run them in Python2, I get UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128). If I add

.. testsetup:: *

   from __future__ import unicode_literals

then I get a lot of errors like

Expected:
    'something'
Got:
    u'something'

Is there a way to have doctest containing unicode characters in the rst files that work unchanged in Python 2.7 and 3.2+?

回答1:

Make sure you are using Python 3.3. It added the explicit u'unicode literals' -- i.e. with u prefix again -- to ease the transition between Python 2 with unicode literals and Python 3. See http://docs.python.org/3/whatsnew/3.3.html#pep-414-explicit-unicode-literals