Python 中的 ANSI 编码

问题描述 投票:0回答:3

在 Windows 上我可以这样做:

>>> "\x98".encode("ANSI")
b'\x98'

在 Linux 上它会抛出一个错误:

>>> "\x98".encode("ANSI")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: ANSI

如何在 Linux 上从

b"\x98"
获得
"\x98"
CPXXXX
编码对我不起作用,因为 \x98 不存在

python encode
3个回答
2
投票

关于ANSI编码的定义

ANSI 编码是一个稍微通用的术语,用于指代系统(通常是 Windows)上的标准代码页。它在西方/美国更恰当地称为 Windows-1252系统。

你可以试试:

"\x98".encode("cp1252")

但是,Python 不允许这样做,您可以使用 ISO Latin1 代替:

"\x98".encode("iso_8859_1")
# b'\x98'

0
投票

我不确定你期望它做什么。 U+0098 是不可打印的字符。您可以尝试

bytes([ord("\x98")])
进行原始翻译。


0
投票

你可以使用这个:

def get_ansi_cp():
    import codecs
    import locale
    return codecs.lookup(locale.getpreferredencoding()).name

在我的机器上返回

cp1252
.

© www.soinside.com 2019 - 2024. All rights reserved.