0

Python represents escape sequences with \ as I understand. So if I tryo to insert a single backslash into a string, I get the string variable with double backslashes as below:

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


>>> b
'\\x91\\x84\\xa4\\x74'

But then If I have two bytes objects - one with single backslash and another with double backslashes, and give them each to pandas.read_msgpack() function, why does it give different outputs in each case? Please see what I have tried below:

byte_obj1 = b'\x91\x84\xa4\x74\x69\x6d\x65\x92\xcb\x41\xdd\xcd\x65\x00\x00\x00\x00\xcb\x41\xdd\xcd\x65\x00\x00\xa3\xd7\xa4\x76\x61\x72\x30\x92\xcb\x40\x49\x0c\xcc\xcc\xcc\xcc\xcd\xcb\x40\x49\x0c\xcc\xcc\xcc\xcc\xcd\xa4\x76\x61\x72\x31\x92\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\x40\x4e\x0c\xcc\xcc\xcc\xcc\xcd\xa4\x76\x61\x72\x32\x92\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\xff\xf8\x00\x00\x00\x00\x00\x00'

d1=pandas.read_msgpack(byte_obj1)
>>> d1 
({'time': (2000000000.0, 2000000000.01), 'var0': (50.1, 50.1), 'var1': (nan, 60.1), 'var2': (nan, nan)},)

byte_obj2=
    b'\\x91\\x84\\xa4\\x74\\x69\\x6d\\x65\\x92\\xcb\\x41\\xdd\\xcd\\x65\\x00\\x00\\x00\\x00\\xcb\\x41\\xdd\\xcd\\x65\\x00\\x00\\xa3\\xd7\\xa4\\x76\\x61\\x72\\x30\\x92\\xcb\\x40\\x49\\x0c\\xcc\\xcc\\xcc\\xcc\\xcd\\xcb\\x40\\x49\\x0c\\xcc\\xcc\\xcc\\xcc\\xcd\\xa4\\x76\\x61\\x72\\x31\\x92\\xcb\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00\\xcb\\x40\\x4e\\x0c\\xcc\\xcc\\xcc\\xcc\\xcd\\xa4\\x76\\x61\\x72\\x32\\x92\\xcb\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00\\xcb\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00'

d2=pandas.read_msgpack(byte_obj2)
>>> d2 
[92, 120, 57, 49, 92, 120, 56, 52, 92, 120, 97, 52, 92, 120, 55, 52, 92, 120, 54, 57, 92, 120, 54, 100, 92, 120, 54, 53, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 52, 49, 92, 120, 100, 100, 92, 120, 99, 100, 92, 120, 54, 53, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 99, 98, 92, 120, 52, 49, 92, 120, 100, 100, 92, 120, 99, 100, 92, 120, 54, 53, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 97, 51, 92, 120, 100, 55, 92, 120, 97, 52, 92, 120, 55, 54, 92, 120, 54, 49, 92, 120, 55, 50, 92, 120, 51, 48, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 52, 48, 92, 120, 52, 57, 92, 120, 48, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 100, 92, 120, 99, 98, 92, 120, 52, 48, 92, 120, 52, 57, 92, 120, 48, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 100, 92, 120, 97, 52, 92, 120, 55, 54, 92, 120, 54, 49, 92, 120, 55, 50, 92, 120, 51, 49, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 102, 102, 92, 120, 102, 56, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 99, 98, 92, 120, 52, 48, 92, 120, 52, 101, 92, 120, 48, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 100, 92, 120, 97, 52, 92, 120, 55, 54, 92, 120, 54, 49, 92, 120, 55, 50, 92, 120, 51, 50, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 102, 102, 92, 120, 102, 56, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 99, 98, 92, 120, 102, 102, 92, 120, 102, 56, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48]

Why does Python not consider double backslahes and '\' same as in case of escape sequence? Could someone please help me in this dilemma. Thank you very much in advance.

1 Answers1

1

In your initial setting, you wrote x = '/x91/x84/xa4/x74'. These are forward slashes and not backward slashes. Backward slashes in python are escape characters, so the first backslash in a double backslash functions as an escape character for the second backslash.

Asad Rauf
  • 743
  • 9
  • 17
  • 1
    Thank you for your reply. That was just for an example. Yes I understand that the first one is an escape character. But then why does messagepack library in pandas not consider the same and give the same output in both the cases? – Aparna Bose Dec 18 '18 at 09:24
  • 1
    byte object 1 has escape character followed by x which means the next two characters are going to get interpreted as hex codes. byte object2 just escapes the backslash itself. if you want to get the same output from both, you will have to write byte_obj1 = b'/x91/x84/xa4/... – Asad Rauf Dec 18 '18 at 10:57