【问题标题】:how to convert "b'\\xfe\\xff\\x002\\x000\\x001\\x009'" to character in python如何将“b'\\xfe\\xff\\x002\\x000\\x001\\x009'”转换为python中的字符
【发布时间】:2018-07-27 07:45:13
【问题描述】:

我有一个程序,它返回如下字符串:b'\\xfe\\xff\\x000\\x008\\x00/\\x001\\x002\\x00/\\x001\\x009\\x009\\x003'
如何将其转换为可读字符串。这个值应该是08/12/1993
所以想象一下我有这样的东西

a = "b'\\xfe\\xff\\x000\\x008\\x00/\\x001\\x002\\x00/\\x001\\x009\\x009\\x003'"
print(a.convert())

【问题讨论】:

    标签: python encoding hex


    【解决方案1】:

    序列\xfe\xff 告诉我们我们有 utf-16(参见http://unicodebook.readthedocs.io/guess_encoding.html

    让我们试试吧:

    x = b'\xfe\xff\x000\x008\x00/\x001\x002\x00/\x001\x009\x009\x003'
    print(x.decode('utf-16'))
    

    给了

    '08/12/1993'    
    

    为了完整性: 如果输入以字符串形式给出,您可以使用eval 将其转换为<class 'bytes'>

    x = eval("b'\\xfe\\xff\\x000\\x008\\x00/\\x001\\x002\\x00/\\x001\\x009\\x009\\x003'")
    
    print(x)   ### b'\xfe\xff\x000\x008\x00/\x001\x002\x00/\x001\x009\x009\x003'
    
    print(x.decode('utf-16'))  ### returns 08/12/1993
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2021-10-11
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多