Python之pickle: Pickle your data
1.What is Pickle? What are the advantages of Pickle?
Python provides a standard module called Pickle. This is an amazing module that can express almost any Python object (even some Python code blocks (forms)!) as a string, a process called pickling. Reconstructing an object from a string representation is called unpickling. Objects in the encapsulated state can be stored in files or objects, and can also be transferred between remote machines over the network.
The Pickle module will create a Python language-specific binary format without converting them into strings or writing them into a binary file without using underlying file access operations. You basically don’t need to consider any files. detail.
2.Pickle’s main functions
The two main functions in the Pickle module are dump() and load(). The
dump() function saves the data object to a given file in a specific format.
When the load() function takes out saved objects from the file, pickle knows how to restore these objects to their original format.
The dumps() function performs the same serialization as the dump() function. Instead of accepting a stream object and saving the serialized data to a disk file, this function simply returns the serialized data.
The loads() function performs the same deserialization as the load() function. Instead of accepting a stream object and reading the serialized data from a file, it accepts a str object containing the serialized data and returns the object directly.
cPickle is a faster C language compiled version of pickle.
pickle and cPickle are equivalent to java's serialization and deserialization operations
3. How to use Pickle
import pickle
with open('mydata.pickle','wb') as mysavedata:
pickle.dump({'alice':0,'clio':8},mysavedata)
with open('mydata.pickle','rb') as myrestoredata:
a_dict = pickle.load(myrestoredata)
print a_dict
Result:
{'clio': 8, 'alice': 0}
For more Python pickle: Pickle your data related articles, please pay attention to PHP Chinese website !

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
