【发布时间】:2017-01-12 13:21:49
【问题描述】:
我编写了一个快速脚本来从保存在 excel 列中的网站地址列表中删除“http://”子字符串。但是函数替换不起作用,我不明白为什么。
from openpyxl import load_workbook
def rem(string):
print string.startswith("http://") #it yields "True"
string.replace("http://","")
print string, type(string) #checking if it works (it doesn't though, the output is the same as the input)
wb = load_workbook("prova.xlsx")
ws = wb["Sheet"]
for n in xrange(2,698):
c = "B"+str(n)
print ws[c].value, type(ws[c].value) #just to check value and type (unicode)
rem(str(ws[c].value)) #transformed to string in order to make replace() work
wb.save("prova.xlsx") #nothing has changed
【问题讨论】:
-
“不起作用”不是解释,您应该描述问题。 会发生什么?您尝试过什么解决方法?