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

Python Pandas处理Excel:文件夹路径匹配产品编码需求求助

Python Excel批量匹配脚本整合求助

我处于Python入门阶段(零基础到基础水平),喜爱编程的简洁性,现求助以下问题:

我有两个Excel表格:

  • Check表格:单列存储文件夹路径(分隔符为反斜杠)
  • Master表格:包含Productcode、wordslist、priority三列

具体需求

  • 读取Check表格的每个单元格,支持从右到左(RL)读取选项
  • 将路径中的每个单词,在Master表格的wordslist列(单元格内为逗号分隔值)中搜索
  • 找到匹配项后,保存对应的Productcode和priority,继续搜索至Master表格末尾
  • 从匹配结果中选取优先级最高的编码(1优先级高于2)
  • 将该编码填入Check表格对应单元格的相邻列
  • 若未找到匹配,按选定方向(从右到左/从左到右)继续搜索路径中的所有单词
  • 若路径中所有单词均未在Master中找到,在相邻列填入'TBD'
  • 处理下一个单元格,重复上述步骤

Master表格数据示例

Productcodewordslistpriority
prdX001folder 1 name, apple, orange, subfolder 2 name1
prdX002folder 1 name, apple, folder 2 name, orange2
prdX003subfolder 1 name, apple, orange, folder 2 name3
prdX004apple, orange, folder 1 name, subfolder 2 name4

Check表格数据示例(从右到左读取)

TestValueCode AppliedReason (仅作理解参考,无需写入Check表格)
\server\fileshare\folder 1 name\subfolder 1 name3(prdx004优先级低,不适用)
\server\fileshare\folder 2 name\subfolder 2 name1

现有代码片段(需整合)

  1. 读取Check表格单元格的代码:
# Loop will print all values of first column
for i in range(2, m_row + 1):
    cell_obj = sheet_obj.cell(row = i, column = 1)
    # print(cell_obj.value)
    path = os.path.normpath(cell_obj.value)
    split_path = path.split('\\')
    for x in range(len(split_path)):
        #Don't like this hard coding
        if x > 3:
            if len(split_path[x]) > 0:
                print(split_path[x])
  1. 搜索Master表格的代码:
# Read Excel file
df = pd.read_excel(path_universal,sheet_name=1, usecols=[1])
for value in df['wordslist']:
    # do something with value
    my_list = df['wordslist'].str.split(',')
    print(tuple(my_list))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 18:13:20