Python doctests and unicode

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:12:50

问题


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



来源:https://stackoverflow.com/questions/17439062/python-doctests-and-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!