This question already has an answer here:
- Converting a String to Dictionary? 7 answers
I have a string output which is in form of a dict ex.
how can make easily save it as a dict and not as a string?
astr is a string which is "in the form of a dict".
This is best if you're on Python 2.6+, as it's not subject to the security holes in eval.
Where are you getting this string from? Is it in JSON format? or python dictionary format? or just some ad-hoc format that happens to be similar to python dictionaries?
If it's JSON, or if it's only a dict, and only contains strings and/or numbers, you can use
This approach has some shortcomings though, if for instance, the strings are enclosed in single quotes
Though I think it's likely the string you're getting is intended to be in JSON format. I make this assumption because it's a common format for data exchange.