My issue:
I need to out every item in a dictionary to an external text file in Python. Like the following:
dict1 = {}
gen1 = 1
aha = 2
dict1['Generation'] = gen1
dict1['Population'] = aha
for key, value in sorted(dict1.items()):
print(key,':',value, file = open('text.txt', 'w'))
In this example I have 2 keys in the dictionary however when I run the code and go to the file the only line which has been made only the first key is outputted.
What Can I do so that all of the keys in the dictionary are printed in the external file?
thank you