我有一个函数的代码部分,从字符串中替换严重编码的外来字符:
s = "String from an old database with weird mixed encodings"
s = str(bytes(odbc_str.strip(), 'cp1252'))
s = s.replace('\\x82', 'é')
s = s.replace('\\x8a', 'è')
(...)
print(s)
# b"String from an old database with weird mixed encodings"
Run Code Online (Sandbox Code Playgroud)
我这里需要一个"真正的"字符串,而不是字节.但是,当我想解码它们时,我有一个例外:
s = "String from an old database with weird mixed encodings"
s = str(bytes(odbc_str.strip(), 'cp1252'))
s = s.replace('\\x82', 'é')
s = s.replace('\\x8a', 'è')
(...)
print(s.decode("utf-8"))
# AttributeError: 'str' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
提前致谢 !
编辑: …