save dict python code example
Example 1: how to save dict in txt format
dict = {'Python' : '.py', 'C++' : '.cpp', 'Java' : '.java'}
f = open("dict.txt","w")
f.write( str(dict) )
f.close()
Example 2: save and load a dictionary python
import pickle
dictionary_data = {"a": 1, "b": 2}
a_file = open("data.pkl", "wb")
pickle.dump(dictionary_data, a_file)
a_file.close()
a_file = open("data.pkl", "rb")
output = pickle.load(a_file)
print(output)
a_file.close()
Example 3: how to modify the dict in python
d = {1: "one", 2: "three"}
d1 = {2: "two"}
d.update(d1)
print(d)
d1 = {3: "three"}
d.update(d1)
print(d)
Example 4: save python dic
import pickledict = {'Python' : '.py', 'C++' : '.cpp', 'Java' : '.java'}f = open("file.pkl","wb")pickle.dump(dict,f)f.close()