gpt4 book ai didi

Python - 如何在替换字符串后保存保存文件

转载 作者:行者123 更新时间:2023-12-04 20:54:18 31 4
gpt4 key购买 nike

我在这里得到一个问题,我在替换其中的字符串后尝试保存 excel 文件,一切正常,但似乎没有写回 excel 文件。谁能给我解释一下?非常感谢!

from xlrd import open_workbook
from xlutils.copy import copy

rb = open_workbook('C:\\Users\\Eric\\Desktop\\test.csv')
wb = copy(rb)
sheet = rb.sheet_by_index(0)
sheet.cell_value(0, 0)

first_row_list = sheet.row_values(0)
name_index = first_row_list.index('Name')
print(name_index)

for i in range(sheet.nrows):
target_column = sheet.cell_value(i, name_index)
print(i)
target_column.replace("a", "b")
print(target_column.replace("a", "b"))
wb.save('C:\\Users\\Eric\\Desktop\\test.csv'`

最佳答案

replace()方法返回替换的字符串,但不更新传递给它的字符串。

只需更换:

target_column.replace("a", "b")

和:
target_column = target_column.replace("a", "b")

关于Python - 如何在替换字符串后保存保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509313/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com