我是一名Python初学者,对于学校来说,我有点不耐烦。我的老师要我写一个函数,返回带有重音的句子,“print()”给我看好字符,带有重音,但doctest没有。
这是我的代码:
def test() :
"""
>>> test()
à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç
"""
print("à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç")
import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS, verbose = True)
正如我所说,印刷确实能正确地向我展示角色。
这是doctest,问题:
Trying:
test()
Expecting:
\xe0 - \xe2 - \xe4 - \xe9 - \xe8 - \xea - \xeb - \xef - \xee - \xf4 - \xf6 - \xf9 - \xfb - \xfc - \xff - \xe7
ok
测试通过,没有失败,但我真的希望Doctest读取这些字符而不显示Unicode Hex Character。
我该如何解决这个问题?
PS:我的老师使用IDE Thonny,所以我自然地跟着他,我知道他不会责怪我(我们,因为我的伙伴没有进一步搜索,只是将'''改为'e' )。答案 0 :(得分:0)
事实上,Thonny确实是问题的一部分。如果我尝试在Thonny中打印带有重音的句子,doctest会尝试它,即使他通过了测试,他仍会打印错误,但是python仍会打印出好的句子。
所以我测试了很多东西,在我的Ubuntu笔记本电脑上,使用Python 3.6,我注意到如果我的print()打印的不仅仅是一个句子,那么就会出现问题。
这是测试代码:
def test():
"""
>>> test()
é é é
"""
print('é é é')
他打印:
Trying:
test()
Expecting:
é é é
ok
1 items had no tests:
__main__
1 items passed all tests:
1 tests in __main__.test
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
如果我在印刷品中添加一个变量,这就是我所拥有的:
def test(var):
"""
>>> test(5)
é é é 5
"""
print('é é é', var)
文档测试:
Trying:
test(5)
Expecting:
é é é 5
**********************************************************************
File "test.py", line 5, in __main__.test
Failed example:
test(5)
Expected:
é é é 5
Got:
('\xc3\xa9 \xc3\xa9 \xc3\xa9', 5)
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 1 in __main__.test
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
我觉得我在Python中滥用打印,我真的不知道为什么,我在互联网上搜索并找到一些解决方案,比如“print(”%s“,name)”来打印一个变量在打印中,但是当我尝试它时显示“%s”而不是用字符串替换它。我想我找到了关于Python 2.x而不是3.x的文档。
所以,如果我向您提供了有关我的问题的更多信息,或者您有解决方案,我很乐意了解为什么doctest会这样做。无论如何,正如我所说,我的老师不会将此视为错误,所以这不是一个重要的问题,我必须在截止日期前解决,只是我关注的一个问题。