4

I have a string which is as follows

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"'

I want to construct a dict from the above string. I tried something as suggested here

split_text = my_string.split(",")
for i in split_text :
    print i

then I got output as shown below:

"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"

I want output as key pair values of a dictionary as given below :

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" }

Basically I want to skip that "," from the sentence and construct a dict. Any suggestions would be great! Thanks in advance!

3
  • Are you sure that is how you want to format my_string? I can't see how that can be a valid string, but then again, python is a weird language so there might be some obscure string formatting I don't know about. Commented Aug 14, 2015 at 19:59
  • Please correct the code you have posted. Your my_string is not a string, or anything else. It is syntactically invalid. Commented Aug 14, 2015 at 19:59
  • @TobLoef : Sorry! my bad! I forgot to single quotes to my_string. I have updated it. Commented Aug 14, 2015 at 20:00

4 Answers 4

8

Your string is almost already a python dict, so you could just enclose it in braces and then evaluate it as such:

import ast
my_dict = ast.literal_eval('{{{0}}}'.format(my_string))
Sign up to request clarification or add additional context in comments.

7 Comments

Oooh! How is this different from eval ?
@d-coder ast.literal_eval just parses the string and returns it if it evaluates to a literal. It doesn't actually execute it, so it's not vulnerable to injection attacks.
Cool! It worked like a charm! I would give you badge for now because I think I can't up vote it. :P
@d-coder you can accept the answer by clicking on the check mark (you might have to wait a bit first.)
Finally one upvote from my side and accepted your answer! :)
|
2
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))

You can do it via finding tuples using re.findall and passing it to dict

4 Comments

Thanks! but I think I will go with @tzaman answer. I bad at regex!
There you go :) regex freaks me out!
I have one more question actually. Should I update this one or post another ? I want to know how store time in milliseconds to be stored in django models field ?
@d-coder if your new question is unrelated to parsing dictionary-like strings, post it separately.
1

You could have also split on ", and stripped the whitespace and ":

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!'}

Comments

0

A different take using dict comprehension, simpler regex and zip(*[iter()]*n):

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'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.