为什么 xlsxwriter 不更新 Pandas 数据框?

Why is the xlsxwriter not updating Pandas dataframe?

我正在尝试替换 excel sheet 中的一组占位符文本,语法如下:

{{text_to_replace}}

例如单元格 A1 可以保存此值;

"We are looking for {{item_placeholder}}"

然后我有一个替换字典:例如

{'item_placeholder': "apples, 'location_placeholder': "UK"}

所以我希望替换的文本看起来像这样:

"We are looking for apples"

也有可能需要在同一个单元格中替换两个占位符,例如;

"We are looking for {{item_placeholder}} in the {{location_placeholder}}

这是 sheet 在迭代之前的样子:

这是预期的输出:

所以我编写了代码来遍历每个单元格(我知道这里有很多关于遍历行的最佳方法的讨论,但最后我选择了 itertuples),但是数据框架未使用新值更新。

workbook = writer.book
worksheet = writer.sheets[sheet_name]

for row in df.itertuples():
    for col_num, value in enumerate(df.columns.values):
        cell_value = df.iloc[row.Index][col_num]
        for var_name, replacement in variables.items():
            if str(replacement):
                if type(cell_value) == str and "{{" + var_name + "}}" in cell_value:
                    cell_value = str(cell_value).replace("{{" + var_name + "}}", str(replacement))
            else:
                raise ValueError(f'"replacement" in dict is mandatory. Missing for {var_name}')
        if type(cell_value) == str:
            worksheet.write(row.Index, col_num, cell_value)

workbook.close()

我确定我遗漏了一些基本的基本知识,但我是 Panda 和 Python 的新手,并且已经查阅了 xlsxwriter here 的文档,这让我认为它应该有效

考虑只阅读电子表格,然后将其重写到文件中。我知道这不是您要具体询问的内容,但我使用与您相同的循环,并且这有效。可能是在您的代码中您只需要使用 str(var_name) 而不是 var_name。只是猜测,因为你没有说是否有错误或只是没有更新。

任何人,这里有两个示例,您可以读取文件,对单个列进行操作,另一个示例可以对整个数据帧进行操作。

使用此数据创建简单的 .xlsx 文件,工作表名称为 'a'

this    {{item_placeholder}} in the {{location_placeholder}}    a   test    to  find    {{item_placeholder}}
this    is  a   test    to  find    {{item_placeholder}}
this    is  {{item_placeholder}}    test    to  find    {{item_placeholder}} in the {{location_placeholder}}

选项 1

replacement = {'item_placeholder': "apples", 'location_placeholder': "UK"}
df = pd.read_excel('test.xlsx', header=None, sheet_name =['a'])['a']

for k, v in replacement.items():
    # print(k,v)
    df.iloc[:, 6] = df.iloc[:, 6].str.replace('{{' + str(k) + '}}', v)
print(df)

      0                                                  1                     2     3   4     5                 6
0  this  {{item_placeholder}} in the {{location_placeho...                     a  test  to  find            apples
1  this                                                 is                     a  test  to  find            apples
2  this                                                 is  {{item_placeholder}}  test  to  find  apples in the UK

选项 2

replacement = {'item_placeholder': "apples", 'location_placeholder': "UK"}
df = pd.read_excel('test.xlsx', header=None, sheet_name =['a'])['a']

for k, v in replacement.items():
    # print(k,v)
    df = df.replace('{{' + str(k) + '}}', v, regex=True)
print(df)

      0                 1       2     3   4     5                 6
0  this  apples in the UK       a  test  to  find            apples
1  this                is       a  test  to  find            apples
2  this                is  apples  test  to  find  apples in the UK