How can I include special characters (tab, newline) in a python doctest result string?

后端 未结 6 1505
甜味超标
甜味超标 2021-02-18 19:06

Given the following python script:

# dedupe.py
import re

def dedupe_whitespace(s,spacechars=\'\\t \'):
    \"\"\"Merge repeated whitespace characters.
    Examp         


        
6条回答
  •  星月不相逢
    2021-02-18 19:38

    TL;DR: Escape the backslash, i.e., use \\n or \\t instead of \n or \t in your otherwise unmodified strings;

    You probably don't want to make your docstrings raw as then you won't be able to use any Python string escapes including those you might want to.

    For a method that supports using normal escapes, just escape the backslash in the backslash-character escape so after Python interprets it, it leaves a literal backslash followed by the character which doctest can parse.

提交回复
热议问题