문제

I have a string that contains printable and unprintable characters, for instance:

'\xe8\x00\x00\x00\x00\x60\xfc\xe8\x89\x00\x00\x00\x60\x89'

What's the most "pythonesque" way to convert this to a bytes object in Python 3, i.e.:

b'\xe8\x00\x00\x00\x00`\xfc\xe8\x89\x00\x00\x00`\x89'
도움이 되었습니까?

해결책

If all your codepoints are within the range U+0000 to U+00FF, you can encode to Latin-1:

inputstring.encode('latin1')

as the first 255 codepoints of Unicode map one-to-one to bytes in the Latin-1 standard.

This is by far and away the fastest method, but won't work for any characters in the input string outside that range.

Basically, if you got Unicode that contains 'bytes' that should not have been decoded, encode to Latin-1 to get the original bytes again.

Demo:

>>> '\xe8\x00\x00\x00\x00\x60\xfc\xe8\x89\x00\x00\x00\x60\x89'.encode('latin1')
b'\xe8\x00\x00\x00\x00`\xfc\xe8\x89\x00\x00\x00`\x89'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top