You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从API返回的异常信息中提取指定detail字段内容

提取API异常中的detail字段内容

情况1:已获取结构化的异常数据

如果你的代码已经拿到异常返回的列表字典结构(比如API响应直接返回JSON并解析为Python对象),直接通过索引和键访问:

# 假设解析后的异常数据存储在error_details变量中
error_details = [{'code': 10010, 'detail': 'Originating number listed in do-not-originate registry D46', 'title': None}]
# 提取并打印detail内容
print(error_details[0]['detail'])

情况2:仅获取到异常字符串

如果只有打印出来的异常文本,需要先处理字符串并转换为Python对象:

import ast

# 原始异常字符串
error_str = "NoneFull details: [{'code': 10010, 'detail': 'Originating number listed in do-not-originate registry D46', 'title': None}]"
# 截取包含数据的部分
data_segment = error_str.split('Full details: ')[1]
# 将字符串转换为Python列表对象
error_data = ast.literal_eval(data_segment)
# 提取并打印detail内容
print(error_data[0]['detail'])

以上两种方法都能输出目标内容:Originating number listed in do-not-originate registry D46

内容的提问来源于stack exchange,提问作者Ace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 15:01:04