The issue with ast.literal_eval()
is that it only takes a single object as input, like a dictionary or a list for example ast.literal_eval('{ "foo": "bar" }')
. But in my files I have multiple dictionaries like this
dic1 = { "foo": "bar" }
dic2 = { "bar": "foo" }
I would like to be able for example to read the python file and change the value of dict2["bar"]
in the file, on disk, without using text manipulation.
with open("config.py") as file:
file["dict2"]["bar"] = "xxx"
or something like that, and have the changes written back to the file.
The issue with pickle is that it is a binary format so if I write that it's no longer a Python module.
I think the only way for me is to use another format, JSON or YAML instead of Python files. Too bad because this would have been very convenient for me.