2

On Windows i can do like this:

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

On Linux it throws an error:

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

How can i get b"\x98" from "\x98" on linux? CPXXXX encodings don't work for me, because \x98 doesnt exist

3

3 Answers 3

3

Regarding this definition of ANSI encoding:

ANSI encoding is a slightly generic term used to refer to the standard code page on a system, usually Windows. It is more properly referred to as Windows-1252 on Western/U.S. systems.

You can try:

"\x98".encode("cp1252")

But, Python doesn’t allow it so, you can use ISO Latin1 instead:

"\x98".encode("iso_8859_1")
# b'\x98'
Sign up to request clarification or add additional context in comments.

2 Comments

UnicodeEncodeError: 'charmap' codec can't encode character '\x98' in position 0: character maps to <undefined>
This is exactly what I need
0

I'm not sure what you're expecting this to do. U+0098 is an unprintable character. You can try bytes([ord("\x98")]) to do a raw translation.

Comments

0

You can use this:

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

Which at my machine returns cp1252.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.