xxxxxxxxxx
dictionary = {'someKey' : 'someValue'}
file_path = 'somePathToFile/someFileName.py'
with open(file_path, 'w') as output_file:
print(dictionary, file=output_file)
xxxxxxxxxx
import json
# Dictionary to be saved
my_dict = {'key1': 'value1', 'key2': 'value2'}
# Open a file for writing
with open('dict.txt', 'w') as f:
# Write the dictionary to the file in JSON format
json.dump(my_dict, f)
xxxxxxxxxx
with open('saved_dictionary.pkl', 'wb') as f:
pickle.dump(dictionary, f)
with open('saved_dictionary.pkl', 'rb') as f:
loaded_dict = pickle.load(f)
xxxxxxxxxx
# Writing dictionary as json
import json
d = {'guitar':'Jerry', 'drums':'Mickey' }
json.dump(d, open('1.json', 'w'))