Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Python3 pickle module

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use the Python3 pickle module". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the Python3 pickle module.

Features of pickle module

1. It can only be used in python, and only the basic data types of python are supported.

2. You can handle complex serialization syntax. (for example, custom class methods, game archives, etc.)

3. When serializing, you only serialize the entire sequence object, not the memory address.

Pickle: used to convert between python-specific types and python data types. Pickle provides four functions: dumps,dump,loads,load

What kind of data can pickle store?

All primitive types supported by python: Boolean, integer, floating point, plural, string, byte, None.

Lists, tuples, dictionaries, and collections of any primitive type.

Functions, classes, instances of classes

Methods commonly used in pickle module 1. Pickle.dump (obj, file, protocol=None,)

The required parameter obj indicates the object to be encapsulated

The required parameter file indicates the file object to be written by obj. File must be opened in binary writable mode, that is, "wb".

The optional parameter protocol indicates the protocol that tells pickler to use. The supported protocols are 0Magi 1, Python 2, and 3, and the default protocol is protocol 3, which is added in SQL 3.

2. Pickle.load (file,*,fix_imports=True, encoding= "ASCII", errors= "strict")

The required parameter file must be opened in binary readable mode, that is, "rb". Other parameters are optional.

3. Pickle.dumps (obj): returns the encapsulated object as a byte object without writing to the file. 4. Pickle.loads (bytes_object): reads the encapsulated object from the byte object and returns three exception types of pickle module

1. PickleError: the exception class that occurs during encapsulation and unwrapping, inherited from Exception

2. PicklingError: an exception that occurs when an object that cannot be encapsulated is encountered, inherited from PickleError

3. UnPicklingError: an exception that occurs during the unpacking of an object, inherited from PickleError

Pickle code example dumps function

Dumps converts data into strings recognized only by python in a special form.

Import pickledata = ['aa',' bb', 'cc'] # dumps converts the data in a special form into a string p_str = pickle.dumps (data) print (p_str) recognized only by the python language

Results:

B'\ x80\ x03] Q\ X00 (X\ X02\ X00\ X00\ x00aaq\ x01X\ x02\ X00\ X00\ x00bbq\ x02X\ x02\ X00\ x00ccq\ x03e.loads function

Loads converts pickle data into python data structure

Mes = pickle.loads (p_str) print (mes)

Results:

['aa',' bb', 'cc'] Thank you for reading, the above is the content of "how to use the Python3 pickle module". After the study of this article, I believe you have a deeper understanding of how to use the Python3 pickle module, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report