【问题标题】:Python - how to save the save the file after replace stringPython - 如何在替换字符串后保存保存文件
【发布时间】:2019-01-01 16:58:24
【问题描述】:

我在这里得到一个问题,我在替换其中的字符串后尝试保存 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'`

【问题讨论】:

  • print(target_column.replace("a", "b")) 效果很好,它可以打印出替换的东西,但它似乎无法存储在 excel 中。此刻的任何帮助都非常感谢!!

标签: python excel file save


【解决方案1】:

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

简单替换:

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

与:

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

【讨论】:

  • 知道了,非常感谢你的帮助,我已经修好了!
猜你喜欢
  • 2011-12-23
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2012-02-03
  • 1970-01-01
相关资源
最近更新 更多