在字符串或列表python中插入单个反斜杠

时间:2018-12-14 11:48:15

标签: python arrays byte

我有一个字节对象

  

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 \ 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 \ x00 \ xcb \ xff \ xf8 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xcb \ x40 \ x3e \ x19 \ x99 \ x99 \ x99 \ x99 \ x9a'“

我想将\\替换为\,以便进一步处理pandas msgpack库。我尝试了很多替换方法,但是尽管很多帖子中都提到了相同的问题,却找不到答案。

我已将其转换为字符串:

  

'x91x84xa4x74x69x6dx65x93xcbx42x2bx5bx8bxb7x00x00x00xcbx42x2bx5bx8bxb7x00x05x1fxcbx42x2bx5bx8bxb7x00x0ax3exa4x76x61x72x30x93xcbx40x24x33x33x33x33x33x33xcbx40x24x33x33x33x33x33x33xcbx40x24x33x33x33x33x33x33xa4x76x61x72x31x93xcbxffxf8x00x00x00x00x00x00xcbx40x34x19x99x99x99x99x9axcbx40x34x19x99x99x99x99x9axa4x76x61x72x32x93xcbxffxf8x00x00x00x00x00x00xcbxffxf8x00x00x00x00x00x00xcbx40x3ex19x99x99x99x99x9a'

我可以插入“/”,但是无法在msgpack中对其进行处理。当我尝试插入“\”时,我再次得到双反斜杠。有人可以帮我吗?

我想要的输出是:

  

b'\ x91 \ x84 \ xa4 \ x74 \ x69 \ x6d \ x65 \ x93 \ xcb \ x42 \ x2b \ x5b \ x8b \ xb7 \ x00 \ 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 \ 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 \ 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 \ x00 \ xcb \ xff \ xf8 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xcb \ x40 \ x3e \ x19 \ x99 \ x99 \ x99 \ x99 \ x9a'

1 个答案:

答案 0 :(得分:0)

您可以简单地做到:

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

现在,不要在控制台中浪费b,它将显示为双\\。如果您想按原样查看b,请写print(b)