Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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()
Comment

python save dictionary to file

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)
Comment

python write a dictionary to file

dictionary = {'someKey' : 'someValue'}
file_path = 'somePathToFile/someFileName.py'
with open(file_path, 'w') as output_file:
    print(dictionary, file=output_file)
Comment

PREVIOUS NEXT
Code Example
Python :: discord.py owner only commands 
Python :: kivy changing screen in python 
Python :: find all unique items in dictionary value python 
Python :: OneHotEncoder sklearn python 
Python :: fake migration 
Python :: pathlib recursive search 
Python :: python string remove whitespace and newlines 
Python :: python pyautogui screenshot 
Python :: python program to multiplies all the items in a list using function 
Python :: mode code python 
Python :: rotatable list python 
Python :: wtform custom validator example 
Python :: python get exception message 
Python :: how to move the pointer on screen using python 
Python :: Pandas groupby max multiple columns in pandas 
Python :: leap year algorithm 
Python :: python every other goes to a list 
Python :: python r before string 
Python :: flask api response code 
Python :: change each line color as a rainbow python 
Python :: open csv file in python 
Python :: python pdf to excel 
Python :: drop multiple columns in python 
Python :: fastest sort python 
Python :: python draw polygon 
Python :: create temporary files in python 
Python :: how to set icon in tkinter 
Python :: how to increase size of graph in jupyter 
Python :: pyautogui pause in python 
Python :: how to change a thread name in python 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =