在Python中将mojibake转换为韩语

2024-06-16 17:02:12 发布

您现在位置:Python中文网/ 问答频道 /正文

(编辑:现在引用Unbaking mojibake

源文件:Android phone.vcf联系人文件 目标:Windows 7用户联系人文件(已导入.vcf)

结果联系人信息: 名称字段的韩语mojibake: '곗퐫李쏀

结果应该只有韩文文本。在做了一点研究之后,我猜有一些与EUC-kr有关的编码问题,因为mojibake中的汉字。但真的不知道。。。在

使用从Unbaking mojibake修改的代码

这是Python的结果: _°ìÔ½çì

显然这是不对的。所以还是卡住了。。。在

# ?'windows-1252' -> 949?
encoding1 = 'ISO-2022-KR' #could not encode
encoding1 = 'windows-1252' #could not encode
encoding1 = 'iso-8859-1' #could not encode
encoding1 = 'utf8' #does nothing
encoding1 = 'euc-kr'

import chardet

import chardet
import codecs

mojibake = '_곗퐫 李쏀__ㅻ━肄섏떎留_'

try:
    encoded_str = mojibake.encode(encoding1)
except UnicodeEncodeError:
    print("error: could not encode")
    encoded_str = None

if encoded_str:
    detected_encoding = chardet.detect(encoded_str)["encoding"]
    print('detected_encoding',detected_encoding)
    try:
        correct_str = encoded_str.decode(detected_encoding)
    except UnicodeEncodeError:
        print("could not decode encoded_str as", detected_encoding)

    print(correct_str)

Tags: importnot联系人encodingencodecouldvcfprint