gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:16:31 25 4
gpt4 key购买 nike

我编写了一个快速脚本,用于从保存在 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

最佳答案

String.replace(substr)

原地不发生,改成:

string = string.replace("http://","")

关于python:为什么替换不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41614323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com