In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use the Pickle library in Python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use the Pickle library in Python" this article.
Brief introduction
There is a serialization process in Python called pickle, which can realize the mutual transformation between arbitrary objects and text, as well as between arbitrary objects and binaries. In other words, pickle can realize the storage and recovery of Python objects.
Comparison between pickle and json
There are basic differences between the pickle protocol and JSON (JavaScript object representation):
JSON is a text serialization format (it outputs unicode text, although most of the time it is encoded as utf-8), while pickle is a binary serialization format
JSON is human readable, while pickle is not.
JSON is interoperable and widely used outside the Python ecosystem, while pickle is specific to Python
By default, JSON can only represent a subset of Python built-in types, and there are no custom classes; pickle can represent an extremely large number of Python types (many of which are automatic, and complex situations can be solved by implementing a specific object api through clever use of Python's introspective facilities).
The application of the pickle module is simple, and there are only four ways
Dumps (): serializes objects in Python into binary objects and returns
Loads (): reads the given binary object data and converts it to a Python object
Dump (): serializes objects in Python into binary objects and writes them to a file
Load (): reads the specified serialized data file and returns the object
Dumps and loads implement memory-based Python object-binary conversion, dump and load implement file-based Python object-binary conversion
Function dumpsdumps (obj, protocol=None)
Obj: the Python object to be converted
The transcoding protocol of protocol:pickle is 0, 1, 2, 3, 4, where 0, 1 and 2 correspond to earlier versions of Python, and 3 and 4 correspond to Python 3.x and later versions. If not specified, the default is 3
Import pickle dic = {"name": "lihua", "age": 30} pe = pickle.dumps (dic) # convert any data type to a binary file print (pe) loadsloads (data)
The data parameter represents the binary object to be converted
Import pickle dic = {"name": "lihua", "age": 30} pe = pickle.dumps (dic) # convert any data type to binary dic1 = pickle.loads (pe) # restore the binary to the original data type print (dic1) dumpdump (obj, file,protocol=None)
Obj: the Python object to be converted
File: convert to the specified binary file, which requires that the file must be operated as "wb"
The transcoding protocol of protocol:pickle is 0, 1, 2, 3, 4, where 0, 1 and 2 correspond to earlier versions of Python, and 3 and 4 correspond to Python 3.x and later versions. If not specified, the default is 3
Import pickle dic = {"name": "lihua", "age": 30} with open ("pickle.pickle", "wb") as f: pickle.dump (dic, f) # stores the converted binary file in the pickle.pickle file load
This function corresponds to the dump () function and is used to convert binary object files into Python objects.
Load (file)
The file parameter represents the binary object file to be converted (the file must be manipulated as "rb" open)
Import pickle dic = {"name": "lihua", "age": 30} with open ("pickle.pickle", "rb") as f: dic1 = pickle.load (f) # Export the contents of the stored file to python object print (dic1). These are all the contents of the article "how to use Pickle libraries in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.