Utilizing Python Text Encodings
Python's text encodings will allow you get your desired result by simply encoding and decoding.
# I have the string shortened for presentation your_string = "\\xda\\xad\\x94" your_string.encode().decode('unicode_escape').encode("raw_unicode_escape")
What is done above can be explained in three simple steps:
- Encode the string in order to turn it into a
bytes object (this allows you to begin the process of removing those pesky backslash escape sequences) - Decode the bytes object into a string with the
unicode_escape codec (this unescapes the backslashes) - Encode the object with
raw_unicode_escape (this will make the string into a bytes object but, as designed, will not escape the backslashes)
Multiple Backslash Escape Sequences
Perhaps you have a string with multiple backslash escape sequences (or double backslashes). If so, you can just repeat steps 2 and 3 as they are listed above as many times as necessary.
your_string = "\\\\xda\\\\xad\\\\x94" your_string.encode().decode('unicode_escape').encode('raw_unicode_escape').decode('unicode_escape').encode('raw_unicode_escape')
As you can see this is getting quite tedious and messy, but you can always create a function to counter that.
Without Backslash Escape Sequences
Now if you have a string without any backslash escape sequences that you want to turn into a bytes object, all that is needed is the encoding seen in step 1:
your_string = "\xda\xad\x94" your_string.encode()
Bytes Objects
If you have a bytes object instead of a string, everything is basically the same, just skip step 1, because bytes objects are already encoded.
your_bytes_obj = b"\\xda\\xad\\x94" your_string.decode('unicode_escape').encode("raw_unicode_escape")
All of these examples should grant you a bytes object without escaped backslashes, which in the examples I have provided above is:
b'\xda\xad\x94'
Explanation
The unicode_escape codec removes escapes when decoding (and alternatively adds escapes when encoding), and the raw_unicode_escape codec does not escape backslashes when encoding. So both of these codecs come in handy when handling escape characters in bytes objects.
raw_unicode_escape
Latin-1 encoding with \uXXXX and \UXXXXXXXX for other code points. Existing backslashes are not escaped in any way. It is used in the Python pickle protocol.
unicode_escape
Encoding suitable as the contents of a Unicode literal in ASCII-encoded Python source code, except that quotes are not escaped. Decode from Latin-1 source code. Beware that Python source code actually uses UTF-8 by default.
I would add that the str.encode() method isn't the only means of encoding a string. Alternatively, you can use the encode function from the codecs module, or even pass your string directly into the built-in bytes(str, encoding) object (just make sure to provide for the correct argument for the encoding parameter).
The reason why I used the str.encode() method here is because it seemed more straightforward.
For more information see:
Python 2 Library - Python Specific Encodings
Python 3 Library - Text Encodings
Python 3 Lexical Analysis - String & Bytes Literals and Escape Sequences