python:为什么replace不工作?

2024-06-02 08:55:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个快速脚本,从保存在excel列上的网站地址列表中删除“http://”子字符串。但是,函数replace不起作用,我不明白为什么。

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

Tags: thetohttpstringwsvaluetypeload