s
= "{'muffin' : 'lolz', 'foo' : 'kitty'}"
-> {'muffin' : 'lolz', 'foo' : 'kitty'}
I want to change the string to dict like this.
My co-developer made me return the string on all functions
When replacing a string with a dict type
How can I do it without using val()
?
In Python 2.6 and later, ast.literal_val(node_or_string) was added to the ast module.
ast.literal_val(node_or_string) is a safer method than val()
Take node
or string
as a factor and return the appropriate string, bytes, numbers, tuples, lists, dicts, sets, booleans, None
values.
import ast
ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}") # = {'muffin': 'lolz', 'foo': 'kitty'}
© 2024 OneMinuteCode. All rights reserved.