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

如何修改Pandas脚本,将ICCID与Nummer按指定格式变量输出?

优化后的脚本及说明

调整后代码

#!/usr/bin/env python3
import pandas as pd

def main():
    path = 'C:\\Users\\test\\Desktop\\'
    
    # 一次性读取Excel文件,避免循环内重复读取浪费资源
    df = pd.read_excel(path + 'alle_08.12.2022.xlsx')
    
    with open(path + 'list.txt') as fp:
        for line in fp:
            target_iccid = line.strip()
            # 筛选匹配ICCID的行
            matched_data = df[df["ICCID"] == target_iccid]
            # 仅当找到匹配结果时输出
            if not matched_data.empty:
                nummer = matched_data.iloc[0]["Nummer"]
                print(f"Nummer {nummer} ICCID {target_iccid}")

if __name__ == '__main__':
    main()

关键修改点

  • 提升效率:将Excel文件读取操作移到循环外部,原脚本每次循环都重新读取Excel,会大幅增加IO开销,调整后仅读取一次即可
  • 格式调整:使用f-string格式化输出,直接提取Nummer和ICCID字段,生成你需要的文本格式
  • 空值处理:增加matched_data.empty判断,避免无匹配结果时输出空内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 12:40:25