Python learns the serialization and deserialization of day4 files,

Source: Internet
Author: User

Python learns the serialization and deserialization of day4 files,

Json and pickle serialization and deserialization

Json is used to implement file interaction between different programs. Because file information interaction is required between different programs, code written in python may need to be transmitted with code written in other languages, json supports interaction between all programs. json replaces XML because the XML format is slightly complicated. Currently, interactions between programs use json for file information interaction.

When json serialization and deserialization are used, the dump operation must be loaded once and cannot be performed.

  

The json serialization process is to write a file and let another programming language call it:

Import json

Info = {"alex": "sb", "test": "hard "}

With open ("file", "w") as f:
F. write (json. dumps (info ))

The above Code uses json to write info dictionary information to a file. The file can only store string format information, or binary file information, and cannot store numbers or other information, the information in the file is of the string type.

Json deserialization process:

Import json

'''Deserialization starts with extracting dump information to achieve interaction between different programming languages '''

With open ("file", "r") as f:
Data = json. loads (f. read ())
Print (data)
Print (type (data ))
Print (data ["alex"])

The code above reads the information stored in json format, as shown below:

{'Test': 'hard ', 'Alex': 'SB '}
<Class 'dict '>
Sb
The code above implements the function of reading string information into the dictionary. In fact, serialization and deserialization are the process of converting the original format into a string first and then reading it, in order to achieve interaction.

We can also use other methods for serialization and deserialization. We know that there is a function eval () that can convert string information to the original style, as shown below:

Info = [11,22, 33,65, 33]

With open ("test. text", "w") as f:
F. write (str (info) # using wirte () can only write string format information to the file, but cannot write other types of information

With open ("test. text", "r") as f_obj:
Data = f_obj.read ()

Data = eval (data)
Print (type (data ))
Print (data)

The program runs as follows:

<Class 'LIST'>
[11, 22, 33, 65, 33]
In the above process, we use the eval () function provided by python to implement the serialization and deserialization processes. However, since serialization and deserialization are implemented in the same program, whether eval () is unknown in other programs, but json supports all programming languages. Therefore, json is usually used to implement information interaction between different programming languages.

Dump and load also implement the functions of dumps and loads above, but in different ways, the syntax is slightly different, as shown below:

Dump serialization:

Import json

Info = {"alex": "sb", "test": "hard "}

With open ("file", "w") as f:
Json. dump (info, f)

Load () deserialization:

Import json

'''Deserialization starts with extracting dump information to achieve interaction between different programming languages '''

With open ("file", "r") as f:
Data = json. load (f)
Print (data)
Print (type (data ))
Print (data ["alex"])

The above program implements the serialization and deserialization functions, dump (information, file path), load (file path), from which the information is read.

Implement data exchange between different programs.

Data exchange between different programs, or converting string information into the original form;

  Eval ()Functions are also very powerful. They can convert string information into original information, as shown below:

>>> Dic = "{'Alex ': 'SB', 'try': 'workhard '}"
>>> Data = eval (dic)
>>> Data
{'Try': 'workhard ', 'Alex': 'SB '}

The program only dumps once, loads once, and cannot dump multiple times. dumps several file implementations;

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.