Reputation: 143
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?
Upvotes: 0
Views: 148
Reputation: 4496
If you're looking to remove the null character \x00
, use replace
:
converted = b.decode('ascii').replace("\x00", "")
With your dictionary code, you get:
{'converted': 'Hello'}
Upvotes: 1