问题
For example, t = str.encode(msg)
print(t) I am getting the double slashes.
b'\\xda\\xad\\x94\\xb4\\x0bg\\x92]R\\x9a1y\\x9d\\xed\\x04\\xd5\\x8e+\\x07\\xf8\\x03\\x1bm\\xd6\\x96\\x10\\xca80\\xe26\\x8a
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.
回答1:
You can't do that because '\\' represent a slash, not a double slash. For example, if you will convert the msg to a string and use the print function to print the msg, you will see only one slash.
回答2:
There are different encoding options that can help you get what you want. Below I encode and decode to get the desired result:
from codecs import encode
# I have the string shortened for presentation
your_string = "\\xda\\xad\\x94"
encode(your_string.encode().decode('unicode_escape'),"raw_unicode_escape")
Now if you have an unescaped string it's even better:
from codecs import encode
your_string = "\xda\xad\x94"
encode(your_string, "raw_unicode_escape")
These both yield a byte of value:
b'\xda\xad\x94'
I have found that other encodings can be used rather than 'raw_unicode_escape'
, but I thought it best to use it.
For more information on encodings see:
Python 2 Standard Encodings
Python 3 Standard Encodings
回答3:
In Python 3.6 having you can use
data_bytes, _ = codecs.escape_decode(data, 'hex')` part of `import codecs
library. In your case data
is msg
variable.
If you print the value of data_bytes
you will get your values in bytes
回答4:
I wanted to place this as a comment to Adrian Gherasims answer, but it got too long so I put it as a separate "answer".
For normal symbols you can use the replace
-function
In [1]: temp = 'aa1aa2aa3aa4aa5'
In [2]: temp
Out[2]: 'aa1aa2aa3aa4aa5'
In [3]: temp.replace('aa', 'a')
Out[3]: 'a1a2a3a4a5'
However if you try to do the same with your double slash it gives a syntax error
In [4]: temp2 = '\\1\\2\\3\\4'
In [5]: temp2
Out[5]: '\\1\\2\\3\\4'
In [6]: temp2.replace('\\', '\')
File "<ipython-input-6-3973ee057a3e>", line 1
temp2.replace('\\', '\')
^
SyntaxError: EOL while scanning string literal
来源:https://stackoverflow.com/questions/38763771/how-do-i-remove-double-back-slash-from-a-bytes-object