将解码字节放入DICT更改为原始字节?
我试图将一些字节解码为ASCII,但是在解码为ASCII并将其放置为dict时,它会更改为字节格式(?)
b = b'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
print(b.decode('ascii')) # Hello
converted = b.decode('ascii')
print(converted, type(converted)) # Hello <class 'str'>
test_dict = {}
test_dict["converted"] = converted
print(test_dict) # {'converted': 'Helloo\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'} ???
有人知道这里发生了什么吗?
I am trying to decode some bytes to ASCII, but when decoding to ASCII and putting it in a dict, it changes back to the bytes format (?)
b = b'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
print(b.decode('ascii')) # Hello
converted = b.decode('ascii')
print(converted, type(converted)) # Hello <class 'str'>
test_dict = {}
test_dict["converted"] = converted
print(test_dict) # {'converted': 'Helloo\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'} ???
Does anyone know what is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想删除 null targine
\ x00
,使用
替换
:使用您的字典代码,您可以得到:
If you're looking to remove the null character
\x00
, usereplace
:With your dictionary code, you get: