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

后端 未结 6 1493
甜味超标
甜味超标 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:41

    I've gotten this to work using literal string notation for the docstring:

    def join_with_tab(iterable):
        r"""
        >>> join_with_tab(['1', '2'])
        '1\t2'
        """
    
        return '\t'.join(iterable)
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    

提交回复
热议问题