gpt4 book ai didi

Python 使用 euc-kr 编码以意想不到的方式对(韩语)字符进行编码(编解码器、编码模块)

转载 作者:行者123 更新时间:2023-12-01 02:31:35 27 4
gpt4 key购买 nike

我尝试在 python 中读取一些以“euc-kr”编码的韩语文本文件,但出现了一些错误。在检查 encodings 模块一段时间后,我了解到该模块以看似非常奇怪的方式对韩语字符进行编码。我举个例子

韩语字符 (这是一个很少使用的字符,但我需要这个作为发音词典)根据 EUC-KR 规范应该编码为 B5 6E (我提到了 this site )。但编码模块给了我一些不同的结果。

# python3
>> from encodings import euc_kr
>> euc_kr.codec.decode(b'\xB5\x6E')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'euc_kr' codec cant decode byte 0xb5 in position 0: illegal multibyte sequence
>> euc_kr.codec.encode('탙')
(b'\xa4\xd4\xa4\xbc\xa4\xbf\xa4\xbc', 1)

如您所见,当我尝试解码 B5 6E 时出现错误,并且 euc_kr.codec.encode 给出的字节比我预期的要长。我不知道那里发生了什么。在解码 B5 6E(以及许多其他韩语字符)时如何避免出现错误?是否有另一个关于 EUC-KR 规范的文档,我可以阅读它来了解 EUC-KR 的 python 实现如何工作?

最佳答案

看起来euc_kr结果是某种分解。您可以尝试cp949,根据Wikipedia :

The default Korean codepage for Windows (code page 949) is a proprietary, but upward compatible extension of EUC-KR...

一些实验:

>>> s = '탇'
>>> ud.name(s)
'HANGUL SYLLABLE TAD'
>>> s.encode('euc_kr')
b'\xa4\xd4\xa4\xbc\xa4\xbf\xa4\xa7'
>>> s.encode('euc_kr').decode('cp949')
'ㅤㅌㅏㄷ'
>>> for c in s.encode('euc_kr').decode('cp949'):
... print(ud.name(c))
...
HANGUL FILLER
HANGUL LETTER THIEUTH
HANGUL LETTER A
HANGUL LETTER TIKEUT
>>> s.encode('cp949').hex()
'b56e'

关于Python 使用 euc-kr 编码以意想不到的方式对(韩语)字符进行编码(编解码器、编码模块),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com