Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove double back slash (`\\`) from a bytes object?

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.

like image 481
Swapnil Avatar asked Oct 30 '22 22:10

Swapnil


1 Answers

Utilizing Python Text Encodings

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:

  1. Encode the string in order to turn it into a bytes object, and to later remove the backslash escape sequences.
  2. Decode the object into a string with the unicode_escacpe codec to unescape the backslashes.
  3. Encode the object with 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'

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 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

like image 197
Darrius Avatar answered Nov 15 '22 06:11

Darrius