例如:
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'任何帮助将不胜感激.
推荐答案有多种编码选项可以帮助您获得所需的内容.下面,我进行编码和解码以获得所需的结果:
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'为了进一步说明问题, unicode_escape 编解码器在解码时会删除转义符,而 raw_unicode_escape 编解码器在编码时不会在字节常量中转义反斜杠.因此,当处理字节文字中的转义字符时,这两种编解码器都派上用场.
To further explain things, the unicode_escape codec removes escapes when decoding, and the raw_unicode_escape codec does not escape backslashes in the byte literals when encoding. So both of these codecs come in handy when handling escape characters in byte literals.
有关编码的更多信息,请参见: Python 2标准编码 Python 3标准编码
更多推荐
如何从字节对象中删除双反斜杠(`\\`)?
发布评论