There are a lot of times when we're going to get some important variables in Python's run, like a dict with a huge amount of data. Also, some of the following programs will use this dict, so we'd better store it locally, and then next time, we'll read the local file, import it into the dictionary type, and call it. This eliminates the process of learning the dictionary again. So how do you store data locally in Python?
What we're using is the pickle module in Python.
As follows:
Import Pickle
data1 = {' A ': [1, 2.0, 3, 4+6j],
' B ': (' string ', U ' Unicode string '),
' C ': None}
Selfref_lis t = [1, 2, 3]
selfref_list.append (selfref_list)
output = open (' Data.pkl ', ' WB ')
# Pickle dictionary using Pro Tocol 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 variables in a program to a local file, and then use Pickle.load to import data from a local file into a variable in the program to implement the call.
Ps:pickle is actually the meaning of kimchi, dump is dumping meaning, it is easier to remember this way.
In fact, the most important thing is to remember the use of the Pickle module ~