有一个函数fix(),作为输出函数的辅助函数,它将字符串写入文本文件.
def fix(line):
"""
returns the corrected line, with all apostrophes prefixed by an escape character
>>> fix('DOUG\'S')
'DOUG\\\'S'
"""
if '\'' in line:
return line.replace('\'', '\\\'')
return line
Run Code Online (Sandbox Code Playgroud)
打开doctests,我收到以下错误:
Failed example:
fix('DOUG'S')
Exception raised:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1254, in __run
compileflags, 1) in test.globs
File "<doctest convert.fix[0]>", line 1
fix('DOUG'S')
^
Run Code Online (Sandbox Code Playgroud)
无论我和我使用什么组合,doctest似乎都不想工作,即使函数本身运行完美.怀疑这是doctest在块评论中的结果,但有任何解决此问题的提示.
这是你想要的吗?:
def fix(line):
r"""
returns the corrected line, with all apostrophes prefixed by an escape character
>>> fix("DOUG\'S")
"DOUG\\'S"
>>> fix("DOUG'S") == r"DOUG\'S"
True
>>> fix("DOUG'S")
"DOUG\\'S"
"""
return line.replace("'", r"\'")
import doctest
doctest.testmod()
Run Code Online (Sandbox Code Playgroud)
原始字符串是你的朋友......
| 归档时间: |
|
| 查看次数: |
1012 次 |
| 最近记录: |