How to store data locally in Python

Source: Internet
Author: User

There are a lot of times when we get some important variables in Python's running process, such as a dict with a large amount of data. Also, some of the subsequent programs will use this dict, then we'd better store it locally, and then the next time you call, read the local file, import into the dictionary type, call. This eliminates the process of re-learning the dictionary. So how do you store data locally in Python?

We are using the Pickle module in Python.
As follows:

ImportPickledata1 = {' A ': [1,2.0,3,4+6j],' 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 variables in the 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 incredibly is the meaning of kimchi, dump is dumping meaning, this kind of memory is easier.

In fact, the main thing is to remember the use of pickle module ~

Reference Links:
Http://www.cnblogs.com/pzxbc/archive/2012/03/18/2404715.html

How to store data locally in Python

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.