String to Dictionary conversion in python

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Tue Sep 19 19:06:53 EDT 2017


On Thursday, September 14, 2017 at 11:01:46 PM UTC-7, santosh.y... at gmail.com wrote:
> Hi,
> 
> Can anyone help me in the below issue.
> 
> I need to convert string to dictionary 
> 
> string = " 'msisdn': '7382432382', 'action': 'select', 'sessionId': '123', 'recipient': '7382432382', 'language': 'english'"
> 
> Can anyone help me with the code

We're not going to do your homework for you.  What have you tried?  What errors are you getting?  If you're replying with your code or an error, make sure to use Copy/Paste and DO NOT just manually re-type the code or error.  Also, make sure to post the entire traceback.

If literal_eval is out of the question, take a look at the str.split() function.  I'd split on a comma, then for each result, split on the colon, then strip out the single quotes.

The actual coding of that is an exercise for the reader.



More information about the Python-list mailing list