Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

python - 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'xdaxadx94xb4x0bgx92]Rx9a1yx9dxedx04xd5x8e+x07xf8x03x1bmxd6x96x10xca80xe26x8a'

Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...