S
Steven D'Aprano
I have a byte-string which is an escape sequence, that is, it starts with
a backslash, followed by either a single character, a hex or octal escape
sequence. E.g. something like one of these in Python 2.5:
'\\n'
'\\xFF'
'\\023'
If s is such a string, what is the right way to un-escape them to single
character byte strings?
I could decode them to unicode first, then encode to ASCII:
'\n'
but this fails for non-ASCII bytes:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in
position 0: ordinal not in range(128)
a backslash, followed by either a single character, a hex or octal escape
sequence. E.g. something like one of these in Python 2.5:
'\\n'
'\\xFF'
'\\023'
If s is such a string, what is the right way to un-escape them to single
character byte strings?
I could decode them to unicode first, then encode to ASCII:
'\n'
but this fails for non-ASCII bytes:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in
position 0: ordinal not in range(128)