wim*_*wim 4 python unicode decoding character-encoding mojibake
当您错误地解码字符时,您如何识别原始字符串的可能候选者?
Ä×èÈÄÄî?è¤ô_üiâAâjâüâpâXüj_10òb.png
Run Code Online (Sandbox Code Playgroud)
我知道这个图像文件名应该是一些日语字符。但是由于对 urllib 引用/取消引用、编码和解码 iso8859-1、utf8 的各种猜测,我一直无法取消并获得原始文件名。
腐败是可逆的吗?
您可以使用 chardet(使用 pip 安装):
import chardet
your_str = "Ä×èÈÄÄî?è¤ô_üiâAâjâüâpâXüj_10òb"
detected_encoding = chardet.detect(your_str)["encoding"]
try:
correct_str = your_str.decode(detected_encoding)
except UnicodeDecodeError:
print("Could not estimate encoding")
Run Code Online (Sandbox Code Playgroud)
结果:???????????????_10? (不知道这是否正确)
对于 Python 3(源文件编码为 utf8):
import chardet
import codecs
falsely_decoded_str = "Ä×èÈÄÄî¦è¤ô_üiâAâjâüâpâXüj_10òb"
try:
encoded_str = falsely_decoded_str.encode("cp850")
except UnicodeEncodeError:
print("could not encode falsely decoded string")
encoded_str = None
if encoded_str:
detected_encoding = chardet.detect(encoded_str)["encoding"]
try:
correct_str = encoded_str.decode(detected_encoding)
except UnicodeEncodeError:
print("could not decode encoded_str as %s" % detected_encoding)
with codecs.open("output.txt", "w", "utf-8-sig") as out:
out.write(correct_str)
Run Code Online (Sandbox Code Playgroud)
总之:
>>> s = 'Ä×èÈÄÄî?è¤ô_üiâAâjâüâpâXüj_10òb.png'
>>> s.encode('cp850').decode('shift-jis')
'?????????????_10?.png'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2920 次 |
| 最近记录: |