How to store data locally in python, python Data Storage
Many times, we will get some important variables during the python running process, such as a dict with a large amount of data. In addition, some later programs will also use this dict, so we 'd better store it locally, and then read the local file the next time we call it, import the data to the dictionary type and call it. This eliminates the need to re-learn the dictionary. So how to store data locally in python?
We use the pickle module in python.
As follows:
Import pickledata1 = {'A': [1, 2.0, 3, 4 +], 'B': ('string', u'unicode string'), 'C ': none} selfref_list = [1, 2, 3] selfref_list.append (selfref_list) output = open ('data. pkl ', 'wb') # Pickle dictionary using protocol 0. pickle. dump (data1, output) # Pickle the list using the highest protocol available. pickle. dump (selfref_list, output,-1) output. close ()
Summary: Use pickle. dump to store the variables in the program to a local file, and then use pickle. load to import data from the local file to the variables in the program for calling.
Ps: pickle is actually the meaning of kimchi, dump is the meaning of dumping, so it is easier to remember.
In fact, the most important thing is to remember how to use the pickle module ~
Reference link:
Http://www.cnblogs.com/pzxbc/archive/2012/03/18/2404715.html