Python doctests 和 unicode [英] Python doctests and unicode

查看:52
本文介绍了Python doctests 和 unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码库,可以在 Python 2.7 和 3.2+ 中保持不变.但是文档 rst 文件中的 doctests 让我很头疼.当我在 Python2 中运行它们时,我得到 UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128).如果我添加

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'

有没有办法让 doctest 在 Python 2.7 和 3.2+ 中保持不变的 rst 文件中包含 unicode 字符?

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

推荐答案

确保您使用的是 Python 3.3.它添加了显式的 u'unicode literals'——即再次带有 u 前缀——以简化带有 unicode 文字的 Python 2 和 Python 3 之间的转换.参见 http://docs.python.org/3/whatsnew/3.3.html#pep-414-explicit-unicode-literals

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

这篇关于Python doctests 和 unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆