Skip to main content

How to Convert Bytes to Dictionaries in Python

Working with data often involves handling byte sequences and converting them to more structured formats like dictionaries.

This guide explores various methods to convert bytes objects to dictionaries in Python, covering techniques using ast.literal_eval(), json.loads(), and the steps involved when dealing with single quotes in bytes strings.

Converting Bytes to Dictionaries with ast.literal_eval()

The ast.literal_eval() method safely evaluates a string containing a Python literal, making it a good choice for converting bytes objects representing Python dictionaries.

from ast import literal_eval

my_bytes = b"{'first': 'tom', 'last': 'nolan'}"
my_dict = literal_eval(my_bytes.decode('utf-8'))

print(my_dict) # Output: {'first': 'tom', 'last': 'nolan'}
print(type(my_dict)) # Output: <class 'dict'>
  • The my_bytes.decode('utf-8') converts the bytes object to a string using UTF-8 decoding.
  • The literal_eval() method evaluates that string as a Python dictionary, ensuring that the input string consists of only basic Python literal values.

Converting Bytes to Dictionaries with json.loads()

If your bytes object contains a valid JSON string (with double quotes), you can use json.loads():

import json

my_bytes = b'{"first": "tom", "last": "nolan"}'
my_dict = json.loads(my_bytes.decode('utf-8'))

print(my_dict) # Output: {'first': 'tom', 'last': 'nolan'}
print(type(my_dict)) # Output: <class 'dict'>
  • The json.loads() function parses the decoded string into a Python dictionary.

Replacing Single Quotes with Double Quotes

If your bytes string uses single quotes (which is not valid JSON), you can replace them with double quotes before using json.loads():

import json

my_bytes = b"{'first': 'tom', 'last': 'nolan'}"
my_str = my_bytes.decode('utf-8').replace("'", '"')
my_dict = json.loads(my_str)
print(my_dict) # Output: {'first': 'tom', 'last': 'nolan'}
print(type(my_dict)) # Output: <class 'dict'>
  • The replace("'", '"') replaces all single quotes with double quotes, making it a valid JSON string.
note

Be cautious when using replace() to modify JSON strings, since this can lead to errors if values can also contain single quotes. literal_eval() is generally a safer option if you are not working with a JSON string.

Converting a Dictionary to Bytes

To convert a dictionary to a bytes object, first convert the dictionary to a JSON string using json.dumps() and then encode that to bytes using encode():

import json

my_dict = {
'first': 'tom',
'last': 'nolan',
'age': 30,
}
my_str = json.dumps(my_dict)
my_bytes = my_str.encode('utf-8')

print(my_bytes)

Output:

b'{"first": "tom", "last": "nolan", "age": 30}'