Let’s say I have the following dictionary in a small application.
dict = {'one': 1, 'two': 2}
What if I would like to write the exact code line, with the dict name and all, to a file. Is there a function in python that let me do it? Or do I have to convert it to a string first? Not a problem to convert it, but maybe there is an easier way.
I do not need a way to convert it to a string, that I can do. But if there is a built in function that does this for me, I would like to know.
To make it clear, what I would like to write to the file is:
write_to_file("dict = {'one': 1, 'two': 2}")
the
reprfunction will return a string which is the exact definition of your dict (except for the order of the element, dicts are unordered in python). unfortunately, i can’t tell a way to automatically get a string which represent the variable name.writing to a file is pretty standard stuff, like any other file write: