Python modules (JSON and pickle modules)

Source: Internet
Author: User
Tags string format

JSON and pickle modules, two modules for serialization

JSON module for conversion between a string and a Python data type

Pickle Module for conversion between Python-specific types and Python data types

Two modules, all available with dumps,dump,loads,load 4 functions

1 ImportJSON2s ='{"Key1": "Value1", "Key2": "Value2"}'  #==> use JSON modules to convert strings to other data types, the quotation marks in the string must be enclosed in double quotation marks3ret = Json.loads (s)#==> loads to other data types by string4 Print(Ret,type (ret))5 6RET = json.load (open ('Ethan.txt','R'))#==> Convert a document (internally a string format) to another Python data type7 Print(Ret,type (ret))#A dictionary-style string in the ==> document8 9L ='[11,22,3,56,75]'Tenresult =json.loads (L) One Print(Result,type (result)) A #Summary: - #json.loads () converts strings that resemble dictionaries, lists, and tuples into dictionaries, lists, tuples - #json.load () converts a document (a string that resembles a dictionary, list, tuple) to a dictionary, list, tuple the  -Di = {"Key1":"value1","Key2":"value2"} -ret = Json.dumps (DI)#==> Converting dictionaries, lists, and tuples to string formats - Print(Ret,type (ret)) +  -Json.dump (Di,open ('Ethan.txt','A +'))#==> Convert dictionaries, tuples, lists into string formats and write to documents +  A ImportPickle at  -D = {'name':'Ethan',' Age': 28} -ret = Pickle.dumps (d)#==> Pickle Converting dictionaries, tuples, and lists into binary - Print(Ret,type (ret)) -  -L = [11,22,3,45,54] inres =pickle.dumps (L) - Print(RES) to  +Pickle.dump (D,open ('Ethan.txt','AB'))#==> converting dictionaries, tuples, and lists to binary write documents -  the #Note dump load do not run together, will error, step by step to *  $f = open ('Ethan.txt','RB')Panax NotoginsengR = Pickle.loads (F.read ())#==> converting binary into dictionaries, lists, tuples - Print(r)

Python modules (JSON and pickle modules)

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.