小白求助Python一下!!

查看 43|回复 2
作者:流年天涯   
python进行批量操作,只执行了一个结果(C2行,SW1),剩下的SW2-SW6没有配置文件出来
代码和excel表
excel表:https://wwt.lanzouj.com/iGdMl2136r8d
[Python] 纯文本查看 复制代码import openpyxl
# 此脚本只能读取一行,需解决读取每行问题
def generate_config_files(excel_path):
    # 打开Excel工作簿
    wb = openpyxl.load_workbook(excel_path)
    ws = wb.active
    # 获取配置模板
    template = []
    for row in ws['A']:
        if row.value is not None:
            template.append(row.value)
    # 读取变量名
    variable_names = [ws.cell(row=1, column=col).value for col in range(3, 9)]
    # 遍历每一组变量值并生成配置文件
    variables = {}
    for col in range(3, 9):  # 从C列到H列
        variable_name = ws.cell(row=1, column=col).value.strip('{}')
        variable_value = ws.cell(row=2, column=col).value
        variables[variable_name] = variable_value
    # 生成配置文本
    config_text = ""
    for row in ws.iter_rows(min_row=1, max_col=26):
        line = row[0].value  # A列的值
        if line is not None:
            for var_name, var_value in variables.items():
                line = line.replace(f'{{{{{var_name}}}}}', str(var_value))
            config_text += line + "\n"
        # 使用NAME变量作为文件名
        config_filename = f"{variables['NAME']}.cfg" if variables['NAME'] else "unnamed.cfg"
        # 写入配置文件
        with open(config_filename, 'w') as config_file:
            config_file.write(config_text)
    # 关闭工作簿
    wb.close()
# 替换下面的字符串为您的Excel文件路径
generate_config_files('conf.xlsx')

配置文件, 您的

LaoShui08   

要使这段代码能正确地为每个设备生成独立的配置文件,你需要在遍历变量时考虑到所有行,而不仅仅是第二行。目前的代码仅处理了固定的一组变量值,即第二行的数据。为了修复这个问题,你可以修改代码,使其遍历所有包含数据的行,并为每一行生成对应的配置文件。
以下是修改后的代码,它会正确地遍历所有行,并为每一行生成一个基于该行变量值的配置文件:
import openpyxl
def generate_config_files(excel_path):
    # 打开Excel工作簿
    wb = openpyxl.load_workbook(excel_path)
    ws = wb.active
    # 获取配置模板中的静态部分
    static_template = []
    for row in ws['A']:
        if row.value is not None:
            static_template.append(row.value)
    # 定义变量名列表
    variable_names = [ws.cell(row=1, column=col).value for col in range(3, 9)]
    # 遍历所有含有数据的行
    for row_index in range(2, ws.max_row + 1):  # 开始于第二行
        variables = {}
        # 读取这一行的变量值
        for col in range(3, 9):  # 从C列到H列
            variable_name = ws.cell(row=1, column=col).value.strip('{}')
            variable_value = ws.cell(row=row_index, column=col).value
            variables[variable_name] = variable_value
        # 生成配置文本
        config_text = ""
        for line in static_template:
            for var_name, var_value in variables.items():
                line = line.replace(f'{{{{{var_name}}}}}', str(var_value))
            config_text += line + "\n"
        # 使用NAME变量作为文件名
        config_filename = f"{variables['NAME']}.cfg" if variables['NAME'] else "unnamed.cfg"
        # 写入配置文件
        with open(config_filename, 'w') as config_file:
            config_file.write(config_text)
    # 关闭工作簿
    wb.close()
# 替换下面的字符串为您的Excel文件路径
generate_config_files('conf.xlsx')
这个版本的代码将会检查工作表中的每一行,并为其中包含有效NAME值的每一行生成一个配置文件。注意,如果某行的NAME单元格为空,则会使用默认的“unnamed.cfg”文件名。如果你不希望这种情况发生,可以添加额外的逻辑来跳过那些没有指定NAME的行。
以下后修复后的运行截图:
[i]
[i]
[i]
如果测试没有问题 楼主请及时结贴
流年天涯
OP
  


LaoShui08 发表于 2024-6-6 23:46
[md]要使这段代码能正确地为每个设备生成独立的配置文件,你需要在遍历变量时考虑到所有行,而不仅仅是第二 ...

非常感谢
您需要登录后才可以回帖 登录 | 立即注册

返回顶部