Python JSON and Pickle modules

Source: Internet
Author: User
Tags string format

Serialization modules: JSON and Pickle

Two modules for serialization

    • JSON, used to convert between string and Python data types
    • Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load

Import jsons = ' {' Key1 ': ' value1 ', ' key2 ': ' value2 '} '     # ==> Convert a string to another data type with a JSON module, the quotation marks in the string must be in double quotes ret = Json.loads (s)                         # ==> loads by string to other data types print (Ret,type (ret)) L = ' [11,22,3,56,75] ' result =json.loads (l) Print ( Result,type (Result) # input result: {' key1 ': ' value1 ', ' key2 ': ' value2 '} <class ' Dict ' >[11, 3, +/--] <class ' list ' &G T

Import Jsonret = Json.load (' ethan.txt ', ' R ')  # ==> Convert the document (internally a string format) to another Python data type print (Ret,type (ret))                    # ==> document is a dictionary-style string # input result: {' key1 ': ' value1 ', ' key2 ': ' value2 '} <class ' Dict ' ># Summary: # json.loads () is used to resemble dictionaries, lists, Tuple's string, converted to a dictionary, list, tuple # json.load () to convert a document (a string that resembles a dictionary, a list, a tuple) to a dictionary, list, tuple

Import Jsondi = {"Key1": "Value1", "Key2": "value2"}ret = Json.dumps (di)                    # ==> Convert dictionaries, lists, tuples to string format print (Ret,type ( RET) # Input result: {"Key1": "Value1", "Key2": "value2"} <class ' str ' >json.dump (di,open (' test2.txt ', ' A + '))    # ==> Convert dictionaries, tuples, lists to string format and write to document # Input results: Test2 file Contents {"Key1": "Value1", "Key2": "Value2"}# Summary: # json.dumps ()  used to convert dictionaries, lists, tuples, to strings # json.dump ()   for converting dictionaries, lists, tuples into documents and convert them to strings

Import pickled = {' name ': ' Ethan ', ' age ': 28}ret = pickle.dumps (d)                       # ==> Pickle convert dictionaries, tuples, lists to binary print (RET, type (ret) ) L = [One, one, 3, two, 54]res = Pickle.dumps (l) print (res) pickle.dump (d, open (' test2.txt ', ' ab '))     # ==> Convert dictionaries, tuples, lists to binary Write to document # Note that dump load does not run together, will error, step by step f = open (' test2.txt ', ' RB ') R = pickle.loads (F.read ())                  # ==> convert binary to Dictionary, list, tuple # R = Pickle.load (f) Print (r) # input result: B ' \x80\x03}q\x00 (x\x04\x00\x00\x00nameq\x01x\x05\x00\x00\x00ethanq\x02x\x03\x00\ X00\x00ageq\x03k\x1cu. ' <class ' bytes ' >b ' \x80\x03]q\x00 (k\x0bk\x16k\x03k-k6e. ') {' name ': ' Ethan ', ' age ': 28}# Summary: # pickle.dumps ()  used to convert dictionaries, lists, tuples, and Binary # pickle.dump () to   a dictionary, list, tuple, into a document and converted to binary # pickle.loads ()  is used to convert binary, to dictionary, list, tuple # pickle.load ()   to convert document binaries to dictionaries, lists, tuples

Python JSON and Pickle modules

Related Article

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.