dict to bytes python

Solutions on MaxInterview for dict to bytes python by the best coders in the world

showing results for - "dict to bytes python"
Miguel Ángel
10 Sep 2020
1# credit to the Stack Overflow user in the source linnk
2# Python3
3
4import ast
5
6byte_str = b"{'one': 1, 'two': 2}"
7dict_str = byte_str.decode("UTF-8")
8my_data = ast.literal_eval(dict_str)
9
10print(repr(my_data))
11>>> {'one': 1, 'two': 2}
Jonah
27 May 2016
1# You can use indent option in json.dumps() to obtain \n symbols:
2
3	import json
4
5	user_dict = {'name': 'dinesh', 'code': 'dr-01'}
6	user_encode_data = json.dumps(user_dict, indent=2).encode('utf-8')
7	print(user_encode_data)
8
9# Output:
10	b'{\n  "name": "dinesh",\n  "code": "dr-01"\n}'