Eric Zhang
0
Q:

python convert b string to dict

# Python3 code to demonstrate 
# convert dictionary string to dictionary 
# using ast.literal_eval() 
import ast 
  
# initializing string  
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' 

# using ast.literal_eval() 
# convert dictionary string to dictionary 
res = ast.literal_eval(test_string) 
  
0
# Python3 code to demonstrate 
# convert dictionary string to dictionary 
# using json.loads() 
import json 
  
# initializing string  
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' 
  
# printing original string  
print("The original string : " + str(test_string)) 
  
# using json.loads() 
# convert dictionary string to dictionary 
res = json.loads(test_string) 
  
# print result 
print("The converted dictionary : " + str(res)) 
5
# python3
import ast
byte_str = b"{'one': 1, 'two': 2}"
dict_str = byte_str.decode("UTF-8")
mydata = ast.literal_eval(dict_str)
print(repr(mydata))
0

New to Communities?

Join the community