简体   繁体   English

将字符串字典转换为字典 python

[英]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.我试过ast.literal_evaljson.loads ,但未能成功。

When I used ast.literal_eval , I am getting the O/P as below:当我使用ast.literal_eval时,我得到的 O/P 如下:

{Source:Test Flow Action}

But this is not in python.但这不在 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.确实是 python。

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}任何方式我都可以获得字典类型 output {'a':1,'b':2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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