Python:在doctest中接受unicode字符串作为常规字符串

2024-06-10 22:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

为一种方法编写doctest,该方法通过在原始词典的键中搜索传递的关键字来缩写词典,并返回新的缩写词典。我的docstring如下所示:

def abbreviate_dict(key_word, original_dict):
    """
    >>> orig_dict = {apple_stems: 2, apple_cores: 5, apple_seeds: 3}
    >>> abbreviate_dict('apple', orig_dict)
    {'cores': 5, 'seeds': 3, 'stems': 2}
    """
   etc.
   return new_dict

函数可以工作,但是当我运行py.测试的doctest,该函数将字符串作为unicode返回,因此测试失败。我不会在函数中以编程方式将字符串切换到unicode,但我知道python2.7返回的是unicode。在

^{pr2}$

如何让doctest确认unicode和常规字符串输出是相同的?在


Tags: 方法函数字符串appleunicode关键字coresdict