Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove quotes in python dictionary

I get a dictionary like following,

{'members': '{"mgt.as.wso2.com": 4100,"as.wso2.com": 4300}', 'subdomain': 'mgt'}

In the key members value is passed within two single quotes. Is there a way to remove those single quotes. I want the dictionary like following,

{'members': {"mgt.as.wso2.com": 4100,"as.wso2.com": 4300}, 'subdomain': 'mgt'}
like image 563
Anuruddha Lanka Liyanarachchi Avatar asked Oct 21 '25 13:10

Anuruddha Lanka Liyanarachchi


1 Answers

Evaluate the string value with literal_eval, and then assign it back to the key:

>>> import ast
>>> d['members'] = ast.literal_eval(d['members'])
>>> d['members']['as.wso2.com']
4300
like image 174
Burhan Khalid Avatar answered Oct 23 '25 03:10

Burhan Khalid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!