我有一个字符串,如下所示
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"'
Run Code Online (Sandbox Code Playgroud)
split_text = my_string.split(",")
for i in split_text :
print i
Run Code Online (Sandbox Code Playgroud)
然后我输出如下所示:
"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"
Run Code Online (Sandbox Code Playgroud)
我希望输出作为字典的键对值,如下所示:
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" }
Run Code Online (Sandbox Code Playgroud)
基本上我想从句子中跳过那个","并构造一个字典.任何建议都会很棒!提前致谢!
你的字符串几乎已经是一个python dict,所以你可以将它括在括号中,然后评估它:
import ast
my_dict = ast.literal_eval('{{{0}}}'.format(my_string))
Run Code Online (Sandbox Code Playgroud)