将字符串转换为python dict

将字符串转换为python dict,python,dictionary,Python,Dictionary,我有一个字符串,如下所示 my_string = '"sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "John"' 我想从上面的字符串构造一个dict。我按建议做了点什么 然后我得到如下所示的输出: "sender" : "md-

我有一个字符串,如下所示

my_string = '"sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "John"'
我想从上面的字符串构造一个dict。我按建议做了点什么

然后我得到如下所示的输出:

"sender" : "md-dgenie"
 "text" : "your dudegenie code is 6632. welcome to the world of dudegenie! your wish
 my command!"     ### finds "," here and splits it here too.
 "time" : "1439155803426"
 "name" : "p"
我希望输出为字典的键对值,如下所示:

my_dict = { "sender" : "md-dgenie",
     "text" : "your dudegenie code is 6632. welcome to the world of dudegenie! your wish, my command!",
     "time" : "1439155803426",
     "name" : "p" }
基本上,我想从句子中跳过那个“,”并构建一个dict。任何建议都会很好!提前谢谢

my_string =' "sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "John"'
import re
print dict(re.findall(r'"([^"]*)"\s*:\s*"([^"]*)"',my_string))
您可以通过使用
re.findall
查找
元组
并将其传递给
dict
来完成此操作。您的字符串几乎已经是python dict了,因此您可以将其括在大括号中,然后这样:


您还可以在
“,
上拆分,并删除空格和


使用dict理解、更简单的正则表达式和
zip(*[iter()]*n)
的不同用法:


您确定这就是您想要格式化我的字符串的方式吗?我看不出这怎么可能是一个有效的字符串,但是python是一种奇怪的语言,因此可能存在一些我不知道的模糊字符串格式。请更正您发布的代码。您的
my_string
不是字符串或其他任何内容。它在语法上无效。@TobLoef:对不起!我的错!我忘了在
我的\u字符串
中加单引号。我已经更新了。哦!这与
eval
有何不同?@d-coder
ast.literal\u eval
只解析字符串,如果它的计算结果是文本,则返回它。它实际上并不执行它,所以它不容易受到注入攻击。酷!它就像一个符咒!我现在会给你们徽章,因为我想我不能投票P@d-你可以通过点击复选标记来接受答案(你可能需要先等一等。)最后,我这边投了一票,接受了你的答案!:)谢谢但我想我会同意@tzaman的回答。我不擅长正则表达式!好了:)正则表达式把我吓坏了!实际上我还有一个问题。我应该更新这个还是发布另一个?我想知道如何在django models字段中以毫秒为单位存储时间?@d-coder如果你的新问题与解析字典式字符串无关,请单独发布。
my_string =' "sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "John"'
import re
print dict(re.findall(r'"([^"]*)"\s*:\s*"([^"]*)"',my_string))
import ast
my_dict = ast.literal_eval('{{{0}}}'.format(my_string))
my_string = '"sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "John"'
print(dict(map(lambda x:x.strip('" ') ,s.split(":")) for s in my_string.split('",')))

{'name': 'John', 'time': '1439155575925', 'sender': 'md-dgenie', 'text': 'your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!'}
import re
my_string = '"sender" : "md-dgenie", "text" : "your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!", "time" : "1439155575925", "name" : "John"'
{k:v for k,v in zip(*[iter(re.findall(r'"(.+?)"',my_string))]*2)}

{'text': 'your dudegenie code is 6326. welcome to the world of dudegenie! your wish, my command!', 'sender': 'md-dgenie', 'name': 'John', 'time': '1439155575925'}