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

如何在Palantir中实现文件循环处理?代码报错求助

问题分析与解决方案

核心错误点

  • 试图通过字符串(如f"output{i}")引用函数内的变量:Python无法将字符串直接解析为对应变量名,必须通过容器(列表/字典)管理相关对象
  • range(1, 1, 3)参数错误:该范围不会生成任何迭代项(start等于stop),无法遍历output1/output2
  • 重复调用.dataframe():冗余操作,且可能引发参数解析错误

修正后的代码

@transform(
    output1=Output("output1"),
    output2=Output("output2"),
    source_df1=Input("source1"),
    source_df2=Input("source2")
)
def compute(output1, output2, source_df1, source_df2):
    # 将output和source对象存入列表,方便循环遍历
    outputs = [output1, output2]
    sources = [source_df1, source_df2]
    
    # 遍历每一组source和output
    for source, output in zip(sources, outputs):
        df = source.dataframe()
        # 过滤数据并写入
        filtered_df = df.filter(df.SNAPSHOT_DATE >= "2024-09-18").coalesce(1)
        output.write_dataframe(
            filtered_df,
            output_format="csv",
            options={'compression': 'gzip', 'header': "True"}
        )

额外说明

  • 如果需要支持更多的output/source对,只需在@transform装饰器中添加对应参数,再把新的对象加入outputs和sources列表即可
  • 优先使用zip()同时遍历数据源和输出对象,避免手动维护索引,更简洁不易出错

内容的提问来源于stack exchange,提问作者Tomáš Ulrich

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 05:03:21