简体   繁体   中英

Convert String dict to dict python

I have a string below:-

d = '"{""Source"":""Test Flow Action""}"'

I need to convert it into a dict as:

d = {"Source":"Test Flow Action"}

I tried ast.literal_eval , json.loads , but was not able to succeed.

When I used ast.literal_eval , I am getting the O/P as below:

{Source:Test Flow Action}

But this is not in python.

Can anyone help me with this?

Thanks

json.loads('"{""Source"":""Test Flow Action""}"'.replace('"{', '{').replace('}"', '}').replace('""', '"'))

Yields a dictionary looking like:

{'Source': 'Test Flow Action'}

which is python, indeed.

I was trying a little modified version of the same question just for knowledge purpose.

dt="{'a':1,'b':2}"
dict(dt.replace('}"','}').replace('{"','{'))

any way I can get dict type output {'a':1,'b':2}

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