I have a dictionary and am trying to write it to a file.
exDict = {1:1, 2:2, 3:3}
with open(\'file.txt\', \'r\') as file:
file.write(exDict)
If you want a dictionary you can import from a file by name, and also that adds entries that are nicely sorted, and contains strings you want to preserve, you can try this:
data = {'A': 'a', 'B': 'b', }
with open('file.py','w') as file:
file.write("dictionary_name = { \n")
for k in sorted (data.keys()):
file.write("'%s':'%s', \n" % (k, data[k]))
file.write("}")
Then to import:
from file import dictionary_name