想用python写一个网址转码查看的方法

#想用python写一个网址转码查看的方法

img

理想的情况是下面:

str = '%E4%BA%8C%E5%8F%89%E6%8E%92%E5%BA%8F%E6%A0%91'
str2 = re.sub(r'%',r'\x',str)
print(str2) 显示结果:二叉排序树

或者:

str2转换为bytes类型,
再.decode('utf8'),
再print

如果re.sub()中替换为r'\x'会出现这样的错误:re.error: bad escape \x at position 0

        str = '%E4%BA%8C%E5%8F%89%E6%8E%92%E5%BA%8F%E6%A0%91'
        str2 = re.sub(r'%',r'\\x',str)
        print(str2)
        print(str2)结果为:\xE4\xBA\x8C\xE5\x8F\x89\xE6\x8E\x92\xE5\xBA\x8F\xE6\xA0\x91

(str2 = re.sub(r'%',r'\x',str))
( 如果用r'\x'会出现这样的错误:re.error: bad escape \x at position 0)
( 替换后的str能不能转换为bytes类型,如下面的by?)

这里的问题就是只能上面先打印出来再手动复制给下面by再解码

      by = b'\xE4\xBA\x8C\xE5\x8F\x89\xE6\x8E\x92\xE5\xBA\x8F\xE6\xA0\x91'
      str3 = by.decode('utf8')
      print(str3)
      print(str3)结果为:二叉排序树