首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python doctest结果字符串中包含特殊字符(制表符、换行符)?

如何在python doctest结果字符串中包含特殊字符(制表符、换行符)?
EN

Stack Overflow用户
提问于 2012-01-12 20:14:49
回答 3查看 26.7K关注 0票数 19

给定以下python脚本:

# dedupe.py
import re

def dedupe_whitespace(s,spacechars='\t '):
    """Merge repeated whitespace characters.
    Example:
    >>> dedupe_whitespace(r"Green\t\tGround")  # doctest: +REPORT_NDIFF
    'Green\tGround'
    """
    for w in spacechars:
        s = re.sub(r"("+w+"+)", w, s)
    return s

该函数在python解释器中按预期工作:

$ python
>>> import dedupe
>>> dedupe.dedupe_whitespace('Purple\t\tHaze')
'Purple\tHaze'
>>> print dedupe.dedupe_whitespace('Blue\t\tSky')
Blue    Sky

但是,doctest示例失败,因为制表符在与结果字符串进行比较之前被转换为空格:

>>> import doctest, dedupe
>>> doctest.testmod(dedupe)

给出

Failed example:
    dedupe_whitespace(r"Green           Ground")  #doctest: +REPORT_NDIFF
Differences (ndiff with -expected +actual):
    - 'Green  Ground'
    ?       -
    + 'Green Ground'

如何对doctest heredoc字符串中的制表符进行编码,以便正确执行测试结果比较?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8834916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档