A simple way to store data in Python to the system local _python

Source: Internet
Author: User

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 ~

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.