0
votes

I have a bytes object

b"'\x91\x84\xa4\x74\x69\x6d\x65\x93\xcb\x42\x2b\x5b\x8b\xb7\x00\x00\x00\xcb\x42\x2b\x5b\x8b\xb7\x00\x05\x1f\xcb\x42\x2b\x5b\x8b\xb7\x00\x0a\x3e\xa4\x76\x61\x72\x30\x93\xcb\x40\x24\x33\x33\x33\x33\x33\x33\xcb\x40\x24\x33\x33\x33\x33\x33\x33\xcb\x40\x24\x33\x33\x33\x33\x33\x33\xa4\x76\x61\x72\x31\x93\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\x40\x34\x19\x99\x99\x99\x99\x9a\xcb\x40\x34\x19\x99\x99\x99\x99\x9a\xa4\x76\x61\x72\x32\x93\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\x40\x3e\x19\x99\x99\x99\x99\x9a'"

I want to replace the \\ to \ for further processing to pandas msgpack library. I tried replacing in so many ways and couldn’t find the answer though the same problem was mentioned in many posts.

I had converted it into a string:

'x91x84xa4x74x69x6dx65x93xcbx42x2bx5bx8bxb7x00x00x00xcbx42x2bx5bx8bxb7x00x05x1fxcbx42x2bx5bx8bxb7x00x0ax3exa4x76x61x72x30x93xcbx40x24x33x33x33x33x33x33xcbx40x24x33x33x33x33x33x33xcbx40x24x33x33x33x33x33x33xa4x76x61x72x31x93xcbxffxf8x00x00x00x00x00x00xcbx40x34x19x99x99x99x99x9axcbx40x34x19x99x99x99x99x9axa4x76x61x72x32x93xcbxffxf8x00x00x00x00x00x00xcbxffxf8x00x00x00x00x00x00xcbx40x3ex19x99x99x99x99x9a'

I can insert “/” but then it cannot be processed in msgpack. When I try to insert “\”, I get double backslashes again. Could anyone please help me with this?

My desired output is:

b'\x91\x84\xa4\x74\x69\x6d\x65\x93\xcb\x42\x2b\x5b\x8b\xb7\x00\x00\x00\xcb\x42\x2b\x5b\x8b\xb7\x00\x05\x1f\xcb\x42\x2b\x5b\x8b\xb7\x00\x0a\x3e\xa4\x76\x61\x72\x30\x93\xcb\x40\x24\x33\x33\x33\x33\x33\x33\xcb\x40\x24\x33\x33\x33\x33\x33\x33\xcb\x40\x24\x33\x33\x33\x33\x33\x33\xa4\x76\x61\x72\x31\x93\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\x40\x34\x19\x99\x99\x99\x99\x9a\xcb\x40\x34\x19\x99\x99\x99\x99\x9a\xa4\x76\x61\x72\x32\x93\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\x40\x3e\x19\x99\x99\x99\x99\x9a'

1
when you say "converted it into a string", do you mean a literal in your source code? downvote wasn't me, but maybe due to lack of formatting or length of examples. a couple of bytes would be fine for understanding this - Sam Mason
1. Formatting 2. Code you tried; may be the reasons..although I have not downvoted it!! - Rahul Agarwal
Your bytes object and desired output looks same!! - Rahul Agarwal
Thank you for your reply. I meant a variable of class "str". Sorry for the incorrect formatting. - Aparna Bose
@RahulAgarwal When I print I get the desired output with single backslash. In the variable it is stored with double backslashes. But the msgpack function needs the input with single backslash.Otherwise I get different outputs - Aparna Bose

1 Answers

0
votes

You can simply do:

x = '/x91/x84/xa4/x74'
b = x.replace(r'/', '\\')

Now just don't wirte b in your console it will show you as double \\. If you want to see b as it is intended write print(b)