gpt4 book ai didi

python - ¥ 字符在 Shift-JIS 中编码/解码后转换为\

转载 作者:行者123 更新时间:2023-12-05 07:47:15 24 4
gpt4 key购买 nike

这怎么可能?这是一个错误吗? (Python 2.7.12 和 Python 3.5.1 中的行为相同。)

In [1]: yen = u'\u00A5'

In [2]: print(yen)
¥

In [3]: yen_after_encoding_decoding = yen.encode('shift-jis').decode('shift-jis')

In [4]: print(yen_after_encoding_decoding)
\

In [5]: yen
Out[5]: '¥'

In [6]: yen_after_encoding_decoding
Out[6]: '\\'

In [7]:

yen 的 shift-jis 编码与反斜杠的 ASCII 编码相同,所以大概是相关的。但还是很奇怪!

最佳答案

Shift_JIS 的字符集是在 JIS(日本工业标准)中定义的。字符编码Shift_JIS半角字符集使用JIS X 0201,全角字符集使用JIS X 0208。

题中的“反斜杠”是ISO/IEC 8859-1(Latin-1)中的半角反斜杠,表示为0x005C。另一方面,JIS X 0201(半角字符集)不包含反斜杠(参见 https://en.wikipedia.org/wiki/JIS_X_0201 )。它在 0x005C 中使用日元符号代替反斜杠。许多日语应用程序(例如日语语言环境中的 Windows 资源管理器)使用日元符号作为反斜杠的等价物,如 C:¥Windows

在这种情况下,代码的行为如下所示:

  • yen 在 Unicode 中是 U+00A5
  • yen.encode('shift-jis')bytes中是0x005C,因为包含了¥在 Shift-JIS 中并且在其编码中是 0x005C
  • .decode('shift-jis')0x005C 转换为 Unicode 中的 U+005C (半角反斜杠),因为 ¥ 相当于反斜杠。

关于python - ¥ 字符在 Shift-JIS 中编码/解码后转换为\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40024053/

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