Basic Python serialization knowledge (json/pickle), jsonpickle

Source: Internet
Author: User

Basic Python serialization knowledge (json/pickle), jsonpickle

We call the process of changing objects (variables) from memory to stored as serialization, such as XML, pickling in Python, and serialization, alling, and flattening in other languages, all mean.

After serialization, The serialized content can be written to the disk or transmitted to other servers over the network. In turn, it is called deserialization to re-read the variable content from the serialized object to the memory, that is, unpickling

Json (JavaScript Object Notation)

A lightweight data exchange format. It is based on a subset of ECMAScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy to read and write, and easy to parse and generate by machines (generally used to increase the network transmission rate ).

If we want to pass objects between different languages, we must serialize the objects to a standard format, such as XML, but a better way is to serialize them to JSON, JSON is a string that can be read by all languages, stored on disks or transmitted over the network. JSON is not only in standard format, but also faster than XML, in addition, it can be read directly on the Web page, which is very convenient. The objects in JSON format are the objects in the standard JavaScript language.

The usage is as follows:

Import json # serialized write (json. dumps () with open('test.txt ', 'w') as f: f. write (json. dumps (dic) # serialized read (json. loads) with open('test.txt ', 'R') as f: print (json. loads (f. read () import jsondic = {'name': 'fanjinbao'} # serialized write (json. dump () with open('test.txt ', 'w') as f: json. dump (dic, f) # serialized read (json. load () with open('test.txt ', 'R') as f: print (json. load (f ))

Pickle

The pickle module of python implements all python data sequences and deserialization. Basically, the function usage is not much different from the JSON module, and the method is also dumps/dump and loads/load. CPickle is the C language version of the pickle module, which is relatively faster. Different from JSON, pickle is not used for data transmission between multiple languages. It is used only for persistence of python objects or for object transfer between python programs, therefore, it supports all python data types.

The pickle deserialization object and the original object are equivalent copy objects, similar to deepcopy.

The usage is as follows:

Import pickledic = {'name': 'fanjinbao'} # serialized write (pickle. dump () with open('test.txt ', 'wb') as f: f. write (pickle. dumps (dic) # serialized read (pickle. loads () with open('test.txt ', 'rb') as f: print (pickle. loads (f. read () import pickledic = {'name': 'fanjinbao'} # serialized write (pickle. dump () with open('test.txt ', 'wb') as f: pickle. dump (dic, f) # serialized read (pickle. load () with open('test.txt ', 'rb') as f: print (pickle. load (f ))

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.