我在Python 2.7中显示警告消息时遇到问题。这是一个最低限度的工作示例:
from warnings import warn
# It is a warning after all...
warn("Goodbye cruel world")
如果我从Windows命令提示符运行它,那么我会收到警告消息,但也会收到源代码,例如
test.py:3: UserWarning: Goodbye cruel world
warn("Goodbye cruel world")
这里发生了什么?如何让它只显示此输出的第一行?
答案 0 :(得分:1)
如果您尝试记录某些内容,logging
module更合适:
import logging
# logging.basicConfig(format='%(message)s')
logging.warn("Goodbye cruel world")