python:为什么替换不起作用?

python:为什么替换不起作用?,python,replace,openpyxl,Python,Replace,Openpyxl,我编写了一个快速脚本,从保存在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)

我编写了一个快速脚本,从保存在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
未在适当位置发生,请将其更改为:

string = string.replace("http://","")
string.replace(旧的、新的[,最大值])
只返回一个它不修改的值
string
。比如说,

>>> a = "123"
>>> a.replace("1", "4")
'423'
>>> a
'123'
必须将字符串重新指定给其修改后的值,如下所示:

>>> a = a.replace("1", "4")
>>> a
'423'
所以在你的情况下,你会想写

string = string.replace("http://", "")
“不起作用”不是一种解释,你应该描述这个问题。发生了什么事?你有没有试着去修复它?
string = string.replace("http://", "")