Last updated: Apr 10, 2024
Reading timeยท3 min
To convert a bytes object to a dictionary:
bytes.decode()
method to convert the bytes to a string.ast.literal_eval()
method to convert the string to a dictionary.literal_eval()
method safely evaluates a string that contains a Python
literal.from ast import literal_eval my_bytes = b"{'first': 'bobby', 'last': 'hadz'}" my_dict = literal_eval(my_bytes.decode('utf-8')) print(my_dict) # ๐๏ธ {'first': 'bobby', 'last': 'hadz'} print(type(my_dict)) # ๐๏ธ <class 'dict'>
We used the bytes.decode()
method to convert the bytes object to a string.
my_bytes = b"{'first': 'bobby', 'last': 'hadz'}" my_str = my_bytes.decode('utf-8') print(my_str) # ๐๏ธ "{'first': 'bobby', 'last': 'hadz'}" print(type(my_str)) # ๐๏ธ <class 'str'>
The bytes.decode() method returns a
string decoded from the given bytes. The default encoding is utf-8
.
The last step is to pass the string to the ast.literal_eval()
method.
The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.
from ast import literal_eval my_bytes = b"{'first': 'bobby', 'last': 'hadz'}" my_dict = literal_eval(my_bytes.decode('utf-8')) print(my_dict) # ๐๏ธ {'first': 'bobby', 'last': 'hadz'} print(type(my_dict)) # ๐๏ธ <class 'dict'>
If the properties in your bytes object are wrapped in double quotes, you can
also use the json.loads()
method.
This is a two-step process:
bytes.decode()
method to convert the bytes to a string.json.loads()
method to parse the string into a dictionary.import json my_bytes = b'{"first": "bobby", "last": "hadz"}' my_dict = json.loads(my_bytes.decode('utf-8')) print(my_dict) # ๐๏ธ {'first': 'bobby', 'last': 'hadz'} print(type(my_dict)) # ๐๏ธ <class 'dict'>
The json.loads() method parses a JSON string into a native Python object.
import json json_str = r'{"name": "bobbyhadz", "age": 30}' my_dict = json.loads(json_str) print(type(my_dict)) # ๐๏ธ <class 'dict'>
However, the method only accepts valid JSON strings.
JSONDecodeError
is raised.If the properties in your bytes object are not wrapped in double quotes, use the
ast.literal_eval()
method.
json.loads()
An alternative approach would be to replace the single quotes in the string with double quotes.
import json my_bytes = b"{'first': 'bobby', 'last': 'hadz'}" my_str = my_bytes.decode('utf-8').replace("'", '"') print(my_str) my_dict = json.loads(my_str) print(my_dict) # ๐๏ธ {'first': 'bobby', 'last': 'hadz'} print(type(my_dict)) # ๐๏ธ <class 'dict'>
We used the str.replace()
method to replace all single quotes in the string
with double quotes.
However, this could go wrong if some of the values also contain single quotes.
The ast.literal_eval()
method safely evaluates the string, so it should be
your preferred approach.
If you need to convert a dictionary to bytes:
json.dumps()
method to convert the dictionary to a JSON string.str.encode()
method to convert the string to bytes.import json my_dict = { 'first': 'bobby', 'last': 'hadz', 'age': 30, } my_str = json.dumps(my_dict) print(my_str) # ๐๏ธ '{"first": "bobby", "last": "hadz", "age": 30}' my_bytes = my_str.encode('utf-8') print(my_bytes) # ๐๏ธ b'{"first": "bobby", "last": "hadz", "age": 30}'
The json.dumps() method converts a Python object to a JSON formatted string.
Once we have a string, we can use the str.encode()
method to convert it to
bytes.
The str.encode() method returns an
encoded version of the string as a bytes object. The default encoding is
utf-8
.
You can learn more about the related topics by checking out the following tutorials: