Python pickle module data object persistence operation

高洛峰
Release: 2016-12-16 11:50:10
Original
1387 people have browsed it

I often encounter some strings, lists, dictionaries and other data obtained during the running of Python programs. I want to save them for a long time for future use, instead of simply putting them in the memory and losing the data when shutting down and powering off. The Pickle module in the python module collection comes in handy, it can convert objects into a format that can be transmitted or stored.

1. Pickle object serialization

The operation process in which the Pickle module converts any Python object into a system byte is called serialization object.

2. Comparison between Pickle and CPickle

The former is a module completely implemented in Python. This CPickle is implemented in C. Its speed is many times faster than pickle. It is generally recommended that as long as there is CPickle in the computer, it should be used use it.

3. The dump() method of the Pickle module

There are two commonly used function methods in the Pickle module, one is called dump() and the other is called load().

In the third part, Snake.com will first explain to you the pickle.dump() method:

The syntax of this method is: pickle.dump (object, file, [usage protocol])

Tips: will be persisted There are three types of data "objects" saved in "files". Index 0 is ASCII, 1 is the old binary protocol, and 2 is the new binary protocol. The difference is that the latter is more efficient.
By default, the dump method uses 0 as the protocol.

4. The load method of the Pickle module

The load() method is just the opposite of the dump() method above. The above is serialized data, and this method is used for deserialization.

Syntax: pickle.load(file)

Tips: Read strings from the "file", deserialize and convert them into Python data objects, and operate them normally like these methods of operating data types.




For more articles related to the persistence operation of data objects in the Python pickle module, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!