简体   繁体   English

Python:doctest口音问题

[英]Python: doctest accent issue

I'm a Python beginner, for school, and I am a little pernickety. 我是一名Python初学者,上学了,我有点生气。 My teacher wants me to write a function, returning a sentence with an accent, "print()" show me the good characters, with the accent, but the doctest doesn't. 我的老师要我编写一个函数,返回带有重音的句子,“ print()”向我展示带有重音的好字符,但doctest却没有。

Here is my code : 这是我的代码:

def test() :
   """
   >>> test()
   à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç
   """
   print("à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç")

import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS, verbose = True)

As I said, print does show me properly the characters. 正如我所说,印刷确实可以正确显示字符。

And here is the doctest, the issue : 这是doctest,问题:

Trying:
test()
Expecting:
\xe0 - \xe2 - \xe4 - \xe9 - \xe8 - \xea - \xeb - \xef - \xee - \xf4 - \xf6 - \xf9 - \xfb - \xfc - \xff - \xe7
ok

The test is passed, without a fail, but I really want the Doctest to read those characters without showing the Unicode Hex Character. 测试通过了,没有失败,但是我真的希望Doctest在不显示Unicode十六进制字符的情况下读取那些字符。

How could I fix this? 我该如何解决?

PS: My teacher use the IDE Thonny, so I naturally followed him, and I know he won't blame me (us, because my mates didn't search further and just change the 'é' to an 'e'). PS:我的老师使用IDE Thonny,所以我很自然地跟随他,而且我知道他不会怪我(我们,因为我的同伴没有进一步搜索,只是将'é'更改为'e')。

So, indeed, Thonny is a part of the issue. 因此,确实,Thonny是问题的一部分。 If I try in Thonny to print a sentence with an accent, the doctest try it and even if he passes the test, he still print an error, BUT python still print the good sentence. 如果我尝试在Thonny中打印带有重音的句子,则doctest会尝试它,即使他通过了测试,他仍然会打印错误,但是python仍会打印出好的句子。

So I tested a lot of things, and on my Ubuntu Laptop, with Python 3.6, I noticed that if my print() printed more than a sentence, there was a problem. 因此,我测试了很多东西,在使用Python 3.6的Ubuntu笔记本电脑上,我注意到,如果我的print()打印的不仅仅是一个句子,那就有问题。

This is the test code : 这是测试代码:

def test():
   """
   >>> test()
   é é é
   """
   print('é é é')

And he 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.

And if I add a variable in the print, this is what I have : 如果我在打印中添加一个变量,这就是我所拥有的:

def test(var):
"""
>>> test(5)
é é é 5
"""
print('é é é', var)

doctest: doctest:

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.

I have the feeling that I'm misusing the print in Python, and I really don't know why, I searched on Internet and found some solutions like "print("%s", name)" to print a variable IN the print, but when I tried it shows me the "%s" instead of replacing it by the string. 我感觉我在Python中滥用了打印件,我真的不知道为什么,我在Internet上搜索了一些解决方案,例如“ print(“%s”,name)”,以便在打印件中打印变量,但尝试时会显示“%s”,而不是用字符串替换。 I guess I found documentation about Python 2.x instead of 3.x. 我猜我找到了有关Python 2.x而不是3.x的文档。

So, if I gave you more information about my problem, or if you have the solution, I would be happy to learn why the doctest do this. 因此,如果我为您提供了有关我的问题的更多信息,或者如果您有解决方案,那么我将很高兴了解doctest为什么这样做。 Anyway, as I said, my teacher won't count this as an error, so it's not an important issue that I have to solve with a deadline, just a problem I focus on. 无论如何,正如我所说,我的老师不会将此视为错误,因此这不是我必须在截止日期之前解决的重要问题,而只是我关注的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM