A simple method to store Python data locally.

Source: Internet
Author: User

A simple method to store Python data locally.

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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.