'\x01'
is a text representation of bytes
in Python 2. '\x01'
is a single byte. Bytes that are in ASCII printable range represent themselves e.g., you see 'O'
instead of '\x4f'
:
>>> b'\x4f\x68\x20\x79\x65\x61\x68\x21\x20\x01'
'Oh yeah! \x01'
To remove all "weird" bytes (to keep characters from string.printable
):
#!/usr/bin/env python
import string
weird = bytearray(set(range(0x100)) - set(map(ord, string.printable)))
print(b'Oh yeah! \x01'.translate(None, weird).decode())
# -> Oh yeah!
string.printable
contains some non-printable characters such as '\t'
(tab), '\n'
(newline). To exclude them too and to leave only printing character:
printing_chars = range(0x20, 0x7e + 1)
weird = bytearray(set(range(0x100)) - set(printing_chars))
print(b'Oh yeah! \x01'.translate(None, weird))
# -> Oh yeah!