gpt4 book ai didi

python - 如何将 "b'\\xfe\\xff\\x002\\x000\\x001\\x00 9'"转换为python中的字符

转载 作者:行者123 更新时间:2023-11-30 22:20:32 26 4
gpt4 key购买 nike

我有一个程序,它返回如下字符串: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())

最佳答案

序列\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

关于python - 如何将 "b'\\xfe\\xff\\x002\\x000\\x001\\x00 9'"转换为python中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48826099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com