Day 4-5 serialization JSON & Pickle

Source: Internet
Author: User
Tags date1

Serialization:

Serialization refers to converting the data type in memory into a string so that it can be stored on the hard disk or transmitted over the network to the remote, because the hard disk or the network can only accept bytes when it is transmitted.

Deserialization:

Convert the character into an in-memory data type.

Two modules for serialization. They are used exactly the same way.

    • 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

Why serialize?

You play the game process, play tired, stop, turn off the game, think about 2 days to play again, 2 days later, the game from the place where you last stopped running, your last game progress is certainly saved on the hard disk, is in what form? A lot of temporary data generated during the game is irregular, maybe when you turn off the game there is exactly 10 lists, 3 nested dictionary data collection in memory, need to save it? How do you save? Turn the list into a multiline multi-column form in a file? What about the nested dictionaries? There is no way to save. Therefore, if there is a way to directly memory data stored on the hard disk, the next time the program starts again, read back from the hard disk, or the original format, it is excellent.

Common methods:

1 ImportJson,pickle2Date = {3     'name':{'name':'Alex',' Age': 22,'Salary': 99999}4 }5 6 7 8D = json.dumps (date)#serializes the data in date. Convert to String9D2 = Json.loads (d)#Deserializes a string into an in-memory type.Ten Print(d2['name'])#output Name of value, {' Age ': $, ' name ': ' Alex ', ' Salary ': 99999}. Instructions deserialization succeeded One Print(Type (d)) Af = open ("Test.json","W") -Json.dump (DATE,F)#serializes and writes the date data to a file object. -f = open ("Test.json","R")#Open the file to be deserialized theDate1 = Json.load (f)#Deserializes a file object. Can be read from a file - Print(date1["name"]) -  - " " + serialization, can dump multiple times, but can not load multiple times! - What is the meaning of turning the data type into a string into memory? + 1. Share your memory data with others via the Internet. A 2. Cross-platform. Defines the rules of interaction between different languages. at  -  - " " -  -  -PK = open ("DATA.PKL","WB")#The Write method is WB in #Print (Pickle.dumps (date) #) # serializes data in date to show bytes data type - pickle.dump (DATE,PK) tod = Open ('DATA.PKL',"RB") +D1 = Pickle.load (d)#deserializing data in a DATA.PKL - Print(D1)#{' name ': {' name ': ' Alex ', ' salary ': 99999, ' age ': '}}

the difference between JSON and pickle:

Json:

Advantages: Cross-language, small size

Cons: Only support int\str\list\tuple\dict

Pickle:

Pros: Designed for Python and supports all Python data types

Cons: can only be used in Python, the storage data occupy large space

Day 4-5 serialization 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.