问题描述
我正在通过IDLE使用Python 3.3。运行以下代码时:
I am using Python 3.3 through the IDLE. While running a code that looks like:
raise KeyError('This is a \n Line break')
其输出:
Traceback (most recent call last):
File "test.py", line 4, in <module>
raise KeyError('This is a \n Line break')
KeyError: 'This is a \n Line break'
我希望它输出带有换行符的消息,如下所示:
I would like it to output the message with the line break like this:
This is a
Line Break
我以前尝试将其转换为字符串或使用os.linesep,但似乎无济于事。有什么方法可以强制我将消息正确显示在IDLE上?
I have tried to convert it to a string before or using os.linesep but nothing seems to work. Is there any way I can force the message to be correctly shown on the IDLE?
如果我提出了 Exception
(而不是 KeyError
),然后输出是我想要的,但是我仍然想提高 KeyError
。
If I raise an Exception
(instead of KeyError
) then the output is what I want, but I would like to still raise a KeyError
if possible.
推荐答案
您的问题与IDLE无关。您看到的行为全部来自Python。从命令行以交互方式运行当前存储库CPython,我们将看到您报告的行为。
You problem has nothing to do with IDLE. The behavior you see is all from Python. Running current repository CPython interactively, from a command line, we see the behavior you reported.
Python 3.7.0a2+ (heads/pr_3947:01eae2f721, Oct 22 2017, 14:06:43)
[MSC v.1900 32 bit (Intel)] on win32
>>> raise KeyError('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'This is a \n Line break'
>>> s = 'This is a \n Line break'
>>> s
'This is a \n Line break'
>>> print(s)
This is a
Line break
>>> raise Exception('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: This is a
Line break
>>> raise IndexError(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: This is a
Line break
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e)
'This is a \n Line break'
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e.args[0])
This is a
Line break
我不不知道为什么KeyError的行为不同于IndexError,但是打印e.args [0]应该适用于所有异常。
I don't know why KeyError acts differently from even IndexError, but printing e.args[0] should work for all exceptions.
差异的原因在中给出,并在其中引用了注释 KeyError
源代码:
The reason for the difference is given in this old tracker issue, which quotes a comment in the KeyError
source code:
/* If args is a tuple of exactly one item, apply repr to args[0].
This is done so that e.g. the exception raised by {}[''] prints
KeyError: ''
rather than the confusing
KeyError
alone. The downside is that if KeyError is raised with an
explanatory
string, that string will be displayed in quotes. Too bad.
If args is anything else, use the default BaseException__str__().
*/
此部分显示在<$ Python源代码的 Objects / exceptions.c
中的c $ c> KeyError_str 对象定义。
This section appears in the KeyError_str
object definition in Objects/exceptions.c
of the Python source code.
我将提到您的问题是这种差异的另一种体现。
I will mention your issue as another manifestation of this difference.
这篇关于关于KeyError中错误消息的新行-Python 3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!