简体   繁体   中英

Python - how to save the save the file after replace string

I got the question right here that I try to save the excel file after I replace the string in it, everything is ok, but it seems not write back into excel file. Can anyone explain this to me? thank you very much!

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'`

The replace() method returns the replaced string but not update the string passed to it.

Simply replace:

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

with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM