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)
For list comprehension lovers, this will write all the key : value pairs in new lines in dog.txt
my_dict = {'foo': [1,2], 'bar':[3,4]}
# create list of strings
list_of_strings = [ f'{key} : {my_dict[key]}' for key in my_dict ]
# write string one by one adding newline
with open('dog.txt', 'w') as my_file:
[ my_file.write(f'{st}\n') for st in list_of_strings ]