Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...