我是一个Python初学者,为了学校,我有点挑剔。我的老师想让我写一个函数,返回一个带重音的句子,"print()“显示正确的字符,带重音,但doctest没有。
下面是我的代码:
def test() :
"""
>>> test()
à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç
"""
print("à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç")
import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS, verbose = True)正如我所说的,print确实正确地向我显示了字符。
这里是doctest,问题是:
Trying:
test()
Expecting:
\xe0 - \xe2 - \xe4 - \xe9 - \xe8 - \xea - \xeb - \xef - \xee - \xf4 - \xf6 - \xf9 - \xfb - \xfc - \xff - \xe7
ok测试通过了,没有失败,但我真的希望Doctest读取这些字符而不显示Unicode十六进制字符。
我该怎么解决这个问题呢?
注:我的老师使用IDE Thonny,所以我自然地跟随了他,我知道他不会责怪我(我们,因为我的朋友没有进一步搜索,只是将“é”改为“e”)。
发布于 2017-09-30 19:19:00
因此,Thonny确实是问题的一部分。如果我在Thonny中尝试打印一个带有重音的句子,doctest会尝试它,即使他通过了测试,他仍然会打印一个错误,但python仍然会打印出正确的句子。
所以我测试了很多东西,在我的Ubuntu笔记本电脑上,使用Python3.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,我真的不知道为什么,我在互联网上搜索并找到了一些解决方案,比如" print ( "%s“,name)”来打印print中的一个变量,但当我尝试时,它显示了“%s”而不是用字符串代替它。我想我找到了关于Python 2.x而不是3.x的文档。
因此,如果我给了您更多关于我的问题的信息,或者如果您有解决方案,我很乐意了解doctest为什么要这样做。无论如何,正如我所说的,我的老师不会把这当作一个错误,所以这不是我必须在最后期限前解决的重要问题,而是我关注的问题。
https://stackoverflow.com/questions/46493994
复制相似问题