Say I have a file called settings.py and it’s content is the following:
{
"translate_tabs_to_spaces": True,
"word_wrap": "true",
"font_face": "Monaco",
"font_size": 14.0,
"highlight_line": True,
"ignored_packages":
[
""
],
"what": '''
This is python file, not json
'''
}
How can I get it into a dict called settings in my main app file app.py?
Thanks.
You can use
ast.literal_eval()to do this safely.Giving us:
Edit:Given the new comment from the asker that suggests that he wants to be able to use...in his config,ast.literal_eval()will not be suitable, as it can’t handle ellipses. It’s not quite clear if this is what he meant, and this is still a good answer to the question that was asked.Turns out the asker was talking about triple quoted strings, which are handled by this correctly.