Home > Net >  [Python]: How to convert Convert key value string without quotes in string to dictionary
[Python]: How to convert Convert key value string without quotes in string to dictionary

Time:06-20

Scenario:

1.How to convert the below key value string pair without quotes to dictionary.

employee='{"Priority":{0:"High"},"Age":{range(0,5):"high"}}'

CodePudding user response:

Using eval to execute the expression will return the dictionary

employee = '{"Priority":{0:"High"},"Age":{range(0,5):"high"}}'

dict_ = eval(employee)
print(dict_)  # {'Priority': {0: 'High'}, 'Age': {range(0, 5): 'high'}}
print(type(dict_))  # <class 'dict'>

  • Related