首页 > 解决方案 > Python:如何存储异常值?

问题描述

我如何将异常的值存储为变量?查看较早的帖子,例如下面的帖子,这些方法在 Python3 中不起作用。不使用 pip 安装包我该怎么办?

我的目标是遍历数据,检查所有内容以查找各种内容,并保留所有错误以放入电子表格中。

较早的帖子:Python:捕获任何异常并将其放入变量中

MWE:

import sys

# one way to do this
test_val = 'stringing'
try:
    assert isinstance(test_val, int), '{} is not the right dtype'.format(test_val)
except AssertionError:
    the_type, the_value, the_traceback = sys.exc_info()


标签: pythonexception

解决方案


except AssertionError as err:

有关解包异常内容等更多详细信息,请参阅本教程该块的底部,以“except 子句可以在异常名称后指定一个变量”开头的段落。


推荐阅读