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

遍历XES事件日志嵌套字典取attributes下concept:name报KeyError如何解决

XES日志trace属性取值KeyError问题解决方案

错误原因

  • XES解析逻辑/版本发生了变化:旧版本解析出的trace为[属性字典, 事件列表]的列表结构,所以可以用索引[0]取到属性;新版本解析出的trace为包含attributes、events两个键的标准字典结构,再用索引[0]取值会直接报错或取到错误内容。
  • 字段命名发生了变化:你明确提到属性下存储的字段为concept:name,旧代码中使用的case:concept:name字段名已失效,自然触发KeyError。

解决代码

基础版(确认结构无误后使用)

print("the transactions that started with 'Offer sent out of the system'")
for trace in filtered_log_offer:
    print(trace['attributes']['concept:name'])

兼容版(避免个别trace结构异常导致程序中断)

print("the transactions that started with 'Offer sent out of the system'")
for trace in filtered_log_offer:
    # 兼容新旧两种trace结构,找不到属性时返回默认值避免报错
    case_attr = trace.get('attributes', trace[0] if isinstance(trace, list) else {})
    # 同时兼容两种字段命名
    case_name = case_attr.get('concept:name') or case_attr.get('case:concept:name', '未知case编号')
    print(case_name)

验证步骤

如果运行后仍然有问题,先执行以下代码打印单条trace的完整结构,确认键名和层级是否匹配:

# 打印第一条trace的完整结构
print(filtered_log_offer[0])
# 单独打印第一条trace的属性部分
print(filtered_log_offer[0].get('attributes', {}))

内容的提问来源于stack exchange,提问作者Sam.H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 05:57:03