Python 更换can';找不到%string文本

Python 更换can';找不到%string文本,python,html,Python,Html,可能是一个非常基本的问题,但是下面的代码无法替换 h = '%3c' c = '<' s = 'blah %3c blah' s.replace(h,c) print(s) h='%3c' c='replace返回一个新字符串。将新字符串保存到旧字符串 s = s.replace(h,c) replace返回一个新字符串。将新字符串保存到旧字符串 s = s.replace(h,c) 字符串是不可变的s.replace不会修改s,而是必须处理结果。对此,应使用urlib.parse.

可能是一个非常基本的问题,但是下面的代码无法替换

h = '%3c'
c = '<'
s = 'blah %3c blah'
s.replace(h,c)
print(s)
h='%3c'

c='
replace
返回一个新字符串。将新字符串保存到旧字符串

s = s.replace(h,c)

replace
返回一个新字符串。将新字符串保存到旧字符串

s = s.replace(h,c)

字符串是不可变的
s.replace
不会修改
s
,而是必须处理结果。对此,应使用
urlib.parse.unquote()
,而不是
str.replace
。这不是HTML编码/解码,而是URL引用/取消引用,区别很重要。字符串是不可变的
s.replace
不会修改
s
,而是必须处理结果。对此,应使用
urlib.parse.unquote()
,而不是
str.replace
。这不是HTML编码/解码,而是URL引用/取消引用,区别很重要。