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 analyze pickle and cPickle in python Core Module

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to analyze pickle and cPickle in python core module, for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more small partners who want to solve this problem find a simpler and easier way.

The data format used by the pickle module is specific to python, and different versions are not backward compatible and cannot be recognized by other languages. To interact with other languages, you can use the built-in json package. Using the pickle module, you can save Python objects directly to files without converting them to strings or writing them to a binary file without the need for low-level file access operations. The pickle module creates a binary format specific to the python language. You basically don't have to worry about any file details. It will help you to complete the read-write exclusive operation cleanly. All you need is a legal file handle.

pickle primary function

The two main functions in the pickle module are dump() and load(). The dump() function takes a file handle and a data object as arguments and saves the data object to the given file in a specific format. When we use the load() function to retrieve saved objects from a file, pickle knows how to restore them to their original format.

The dumps() function performs the same serialization as dump(), but unlike dump dumps does not write the converted string to a file, but returns the resulting converted data as a string.

The loads() function performs the same deserialization as the load() function. Loads takes a string argument and decodes the string into a python data type. Functions loads and dumps perform reciprocal operations.

cPickle is a faster C compiled version of pickle.

The dump and load of pickle are equivalent to Java serialization and deserialization operations

Pickle Usage Example #! /usr/local/env python # -*- coding=utf-8 -*- if __name__ == "__main__": import cPickle #Serialize to file obj = 123,"abcdedf",["ac",123],{"key":"value","key1":"value1"} print obj #Output: (123, 'abcdedf',[' ac', 123], {'key1':' value1','key':' value'}) #r Read and write permissions r b Read and write to binary file f = open (r"d:\a.txt","r ") cPickle.dump(obj,f) f.close() f = open(r"d:\a.txt") print cPickle.load(f) #Output: (123, 'abcdedf', ['ac', 123],'key1': 'value1', 'key': 'value'}) #Serialize to memory (string format saved), then object can be processed in any way such as transmission over network obj1 = cPickle.dumps(obj) print type(obj1) #Output: print obj1 #Output: python-specific storage format obj2 = cPickle.loads(obj1) print type(obj2) #Output: print obj2 #Output: (123, 'abcdedf', ['ac', 123], {'key1': 'value1', 'key': 'value'})

Of course, you dump a few times, that is, you need to load a few times, don't hope to dump three times, load returns you a list.

About how to carry out python core module pickle and cPickle analysis of the answer to the problem shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

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

Internet Technology

Wechat

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

12
Report