For example:
t = str.encode(msg)
print(t)
I am getting double slashes, like this:
b'\\xda\\xad\\x94\\xb4\\x0bg\\x92]R\\x9a1y\\x9d\\xed\\x04\\xd5\\x8e+\\x07\\xf8\\x03\\x1bm\\xd6\\x96\\x10\\xca80\\xe26\\x8a
But, I would like to get the result as:
b'\xda\xad\x94\xb4\x0bg\x92]R\x9a1y\x9d\xed\x04\xd5\x8e+\x07\xf8\x03\x1bm\xd6\x96\x10\xca80\xe26\x8a'
Any help would be appreciated.
There are text encodings that can help you get what you want both simply and easily.
Below I encode and decode to get the desired result:
# 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:
bytes
object, and to later remove the backslash escape sequences.unicode_escacpe
codec to unescape the backslashes.raw_unicode_escape
to turn it back into a bytes object without additional escaping.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')
This could get 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 already have an encoding (otherwise an error is raised).
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 of object without escaped backslashes, which in the examples I have provided above is:
b'\xda\xad\x94'
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 the bulit-in bytes
function (just make sure to provide 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With