编辑:
我有一些用Python
编写的代码,并使用pandas生成和操作一些数据帧。在其中一些,我得到一些警告。例如,设置CopyWarning或performancewarning。我想捕捉发生警告的行no,为此,我编写了以下代码。我什么都听懂了,除了lineno。你知道吗
scripts = ['myfile.py', 'myotherfile.py']
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("default")
for s in scripts:
with open(s) as f:
try:
exec(f.read())
except Exception as e:
print('An Error happend during the execution', e)
raise
finally:
f.close()
print(color.orange('There are {} error/s happend in {}.'.format(len(w), s)))
for i in range(0, len(w)):
print(color.green('LineNo: '), w[i].lineno)
print(color.green('Line: '), w[i].line)
print(color.green('warning category: '), w[i].category.__name__)
print(color.green('Warning: '), w[i].message)
print(color.green('filename: '), w[i].file)
print(color.cyan('-' * 40))
对于w[i].lineno
,我期望行号。你知道吗
我得到的号码不属于原始文件。它属于python核心模块。我得到的是5312。 如何将行号追溯到我自己的脚本?你知道吗
当} 。你知道吗
msg.line
丢失时,用来查找源代码行的warnings module uses ^{你也可以这么做:
印刷品
相关问题 更多 >
编程相关推荐