簡體   English   中英

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

[英]Python - how to save the save the file after replace string

我在這里得到一個問題,我在替換字符串后嘗試保存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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM