简体   繁体   中英

How to convert a complex string to dict of dict in python

I have a string with complex dict. I want to convert that as dict of dict recursively

eg: s = "{'key1':\\"{'status': 'completed','media': {'key2':{'value1': False, 'key3': False},'key4':{'active': True}\\"}"

As I am new to python, I couldnt crack this.

Thanks in advance

To read in the string, take a look at the json library:

import json
s = "{'key1':\"{'status': 'completed','media': {'key2':{'value1': False, 'key3': False},'key4':{'active': True}\"}"
imported_dict = json.loads(s)

Then, you can use imported_dict to re-form to your new dictionary format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM