Day18 JSON and Pickle

Source: Internet
Author: User
Tags string format

First, serialization

Purpose: To transform data from memory into a process that can be stored or transmitted

Cause: The program is executed in a series of state changes, this series of "state" in the programming language in the various data structures stored in memory, and memory can not permanently save data, power outage or restart, in memory about the time of the data will disappear all; In order to persist the in-memory data to a file, it is necessary to serialize the in-memory data to be written directly to the file.

Pros: 1. The status of the data can be saved permanently

2. Better cross-platform data interaction capabilities

Second, JSON and pickle

1.json instances

Dictionary serialization

Import jsondic={"name": "" "Luffy,"Age":  Print(type (DIC))   #<class "dict" >#  through JSON Serialization becomes a string format j=json.dumps (DIC)print(Type (j))    #<class "str" >
dic={"name":"Namy"," Age": 18}f=open ("Serializing Objects","W") F.write (j)#= = Json.dump (dic,f)F.close#----Deserialization of----#Importjsonf=open ("Serializing Objects") msg=json.loads (F.read ())#= = Msg=json.load (f)

Serialization of lists

list=["how","old","is"," you "]j=json.dumps (list)print(Type (j))

2.pickle

Serialization of

Import pickledic={"Sunday":"Sun","Sweet  ":"cake"}j=pickle.dumps (DIC)  # format the dictionary as "bytes"

Deserialization

Import picklef=open (" serialized object _pickle","RB") msg= Pickle.loads (F.read ())   #= = Msg=pickle.load (f)

Day18 JSON and 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.