【发布时间】:2019-05-03 10:57:01
【问题描述】:
我正在运行一个程序并得到预期的 ValueError 输出:
ValueError: {'code': -123, 'message': 'This is the error'}
我无法弄清楚如何解析这些数据并只获取代码(或消息)值。如何才能获得 ValueError 的 code 值?
我尝试了以下方法:
-
e.codeAttributeError: 'ValueError' object has no attribute 'code'
-
e['code']TypeError: 'ValueError' object is not subscriptable
-
json.loads(e)TypeError: the JSON object must be str, bytes or bytearray, not 'ValueError'
这样做的pythonic方式是什么?
编辑
做的一件事是获取字符串索引,但我不想这样做,因为我觉得它不是很pythonic。
【问题讨论】:
标签: python python-3.x error-handling valueerror