Coder Perfect

Is it possible to convert a Dictionary’s String representation to a Dictionary?


How do I turn a dict’s str representation, such as the string below, into a dict?

s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"

I prefer to avoid using eval. What other options do I have?

One of my coworker’s classes, which he built, turns all input into strings, which is the main reason for this. I’m not in the mood to go into his classes and change them to deal with this problem.

Asked by UberJumper

Solution #1

The built-in ast.literal eval: method can be used.

>>> import ast
>>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")
{'muffin': 'lolz', 'foo': 'kitty'}

This is a better option than eval. As the company’s own documents state:

>>> help(ast.literal_eval)
Help on function literal_eval in module ast:

    Safely evaluate an expression node or a string containing a Python
    expression.  The string or node provided may only consist of the following
    Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
    and None.

For example:

>>> eval("shutil.rmtree('mongo')")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
  File "/opt/Python-2.6.1/lib/python2.6/", line 208, in rmtree
    onerror(os.listdir, path, sys.exc_info())
  File "/opt/Python-2.6.1/lib/python2.6/", line 206, in rmtree
    names = os.listdir(path)
OSError: [Errno 2] No such file or directory: 'mongo'
>>> ast.literal_eval("shutil.rmtree('mongo')")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/Python-2.6.1/lib/python2.6/", line 68, in literal_eval
    return _convert(node_or_string)
  File "/opt/Python-2.6.1/lib/python2.6/", line 67, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

Answered by Jacob Gabrielson

Solution #2

Although JSON’s decoder requires double quotes around keys and values, it can address the problem. If you don’t mind a little replacing…

import json
s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
# d = {u'muffin': u'lolz', u'foo': u'kitty'}

NOTE: If your keys or values contain single quotes, this will fail due to inappropriate character replacement. If you have a severe objection to the eval approach, this is the solution to use.

More information on the json single quote: Due to an escaping single quote in JSON, jQuery.parseJSON produces a “Invalid JSON” error.

Answered by 0x539

Solution #3

using json.loads:

>>> import json
>>> h = '{"foo":"bar", "foo2":"bar2"}'
>>> d = json.loads(h)
>>> d
{u'foo': u'bar', u'foo2': u'bar2'}
>>> type(d)
<type 'dict'>

Answered by tokhi

Solution #4

To OP’s example:

s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"

To cope with this type of non-standard json in a string, we can use Yaml:

>>> import yaml
>>> s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
>>> s
"{'muffin' : 'lolz', 'foo' : 'kitty'}"
>>> yaml.load(s)
{'muffin': 'lolz', 'foo': 'kitty'}

Answered by lqhcpsgbl

Solution #5

To summarize:

import ast, yaml, json, timeit

descs=['short string','long string']

for  desc,string in zip(descs,strings):
    for  func in funcs:
        print(func.__module__+' '+func.__name__+':')
        %timeit func(string)        


*** short string ***

json loads:
4.47 µs ± 33.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
builtins eval:
24.1 µs ± 163 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
ast literal_eval:
30.4 µs ± 299 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
yaml load:
504 µs ± 1.29 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

*** long string ***

json loads:
29.6 µs ± 230 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
builtins eval:
219 µs ± 3.92 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
ast literal_eval:
331 µs ± 1.89 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
yaml load:
9.02 ms ± 92.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Conclusion: prefer json.loads

Answered by Anatoly Alekseev

Post is based on