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

如何将自动生成的字典结构传入JSON Schema定义函数?

解决方案

直接用字典推导式动态生成yield的字典结构就行,不用手动逐个写键值对。核心是处理自动生成字典里的带引号键,再把字符串形式的属性名转换成实际的row属性访问:

def split_line_items(df_transactions):
    # 自动生成的字典
    d = {'"count_0"': 'row.count_0',
         '"count_1"': 'row.count_1',
         '"count_2"': 'row.count_2',
         '"total_count"': 'row.total_count'}
    
    for row in df_transactions.itertuples():
        # 动态生成目标字典
        yield {
            key.strip('"'): getattr(row, key.strip('"')) 
            for key in d
        }

细节说明

  • key.strip('"'):把字典键首尾的双引号去掉,得到真正的属性名(比如count_0)
  • getattr(row, 键名):安全地获取row对象的对应属性值,比用eval更稳妥,避免潜在安全风险

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 19:35:21