JSON & Pickle

Source: Internet
Author: User

JSON & Pickle

JSON: Typically used for serialization and deserialization between different programming languages, the core of which is the string, which is the conversion between the string and the basic data type of Python

The JSON module provides four functions:dumps, dump(serialization, Save), loads(deserialization, read), load

#json.loads is used to convert strings in dictionaries, lists, and elements into corresponding dictionaries, lists, tuples#for the contents of the dictionary, there must be ""#Example:L ="[11,22,33,44]"ImportJsonresult=json.loads (L)Print(Result,type (result))#output: [One, all, <class] ' list ' >json.dumps ()#convert Python's basic data type to stringUser_list = [11,22,33,44]Importjsons=json.dumps (user_list)Print(S,type (s))

Pickle: A basic data type that is specific to Python and a conversion between sequences that only Python can recognize, which is often the bytes type. Pickle is powerful in the ability to serialize code, class

The Pickle module provides four functions:dumps, dump(serialization, Save), loads(deserialization, read), Load

ImportPickledata= {"K1": 123,"K2":'Hello'}#pickle.dumps converts data into unique Python-specific types through special formsresult =pickle.dumps (data)Print(result, type (result))#B ' \x80\x03}q\x00 (x\x02\x00\x00\x00k1q\x01k{x\x02\x00\x00\x00k2q\x02x\x05\x00\x00\x00helloq\x03u. ' <class ' bytes ' >#pickle.dump converts data into unique Python-specific types and writes to a fileWith open ('Dbb.txt','WB') as F:pickle.dump (data, F)#pickle.loads to restore Python-specific types to the original dataNew_data =pickle.loads (Result)Print(New_data, type (new_data))#{' K1 ': 123, ' K2 ': ' Hello '} <class ' Dict ' >#Pickle.load read from a file that only Python-specific types are restored to the original dataWith open ('Dbb.txt','RB') as F:new_data=pickle.load (f)Print(New_data, type (new_data))#{' K1 ': 123, ' K2 ': ' Hello '} <class ' Dict ' >

JSON & Pickle

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.