I have a string
"{a:'b', c:'d',e:''}"
Please not that the keys to the dictionary entries are unquoted, so a simple eval("{a:'b', c:'d',e:''}")
as suggested in a previous question does not work.
What would be the most convenient way to convert this string to a dictionary?
{'a':'b', 'c':'d', 'e':''}
If this is from a trusted source (do not use this for general user input, as eval is not secure; but then again, if you're getting input from a potentially malicious user you should use JSON format and the json
module instead), you can use eval
with a trick.
source = """{e: '', a: 'b', c: 'd'}"""
class identdict(dict):
def __missing__(self, key):
return key
d = eval(source, identdict())
print(d)
prints
{'a': 'b', 'c': 'd', 'e': ''}
How this works is that we create a new dictionary subclass identdict
that defines the magic method __missing__
. This method is called for lookups on keys that are missing from the dictionary. In this case, we just return the key, so the dictionary maps keys to themselves. Then the source
is eval
uated using an identdict
instance as the globals
argument. eval
will look up the values of variables from the globals
mapping; as it is an identdict
, the value of each variable accessed is conveniently now the name of the variable.
Works for even more complex strings as values, and anything that is proper Python literal syntax.
Depending on the complexity of what you're parsing, this could work:
s = "{a:'b', c:'d',e:''}"
d = dict([
(x.split(':')[0].strip(), x.split(':')[1].strip("' "))
for x in s.strip("{}").split(',')
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With