#!/usr/bin/env python# -*- coding: utf-8 -*-' serialization (pickle) and json1, what is serialization? Serialization allows you to serialize an object, such as a list, a dictionary, as an object, through a python-specific mechanism. That is, to encrypt it in the form of a binary (special binary encryption), the process is serialization and after an object is serialized (such as a class, a list, a dictionary), it can be deserialized. "' import pickle,jsonli = [' Tantianran ', 11,22, ' OK ', ' JJ ', ' #比如把一个列表序列化一下dumpsed = Pickle.dumps (LI) #通过特殊的二进制形式序列化print dumpsedprint type (Pickle.dumps (LI)) #查看数据类型后是一个字符串形 # That is, a list is serialized into a string form, and the string is deserialized as an irregular #, deserialized into List form loadsed = pickle.loads (dumpsed) #把序列化后的数据存储在变量里的这个变量拿过来进行反序列化print loadsedprint type (loadsed) "" is interpreted as follows: 1, originally a list, Later serialized into a String 2, and then deserialized by a string into the list by pickle serialization features: 1, pickle can not only serialize a list, you can also serialize a class, and so on object 2, what data type before serialization, After the deserialization is what data type 3, can also be directly serialized, saved to a file, later read the file, and then deserialized into the original data type 4, to a certain extent, can also be said to do a simple encryption function "' # If the list is serialized and saved to the file Pickle.dump (Li,open (' data.pk ', ' W ')) #将列表序列化后存储到文件里result = pickle.load (open (' Data.pk ', ' R ')) Print resultprint type (Result) #查看数据类型 ' pickLe application scenario 1, the Python program and the data transfer between the Python program, passed in a serialized way 2, in this way to achieve the two Python program between the memory data between the interaction how to say? Two separate processes in memory, the memory space is completely independent and cannot be accessed from one another. If you want to exchange data between two programs, you have to go through the process of serialization. 3, memory data can also be stored in the file after serialization to the hard disk (such as game Progress saved) 4, socket programming, transmission between two computers or need to serialize "" "#pickle和json的区别1, pickle can only be used in Python, Python does its own 2, JSON all languages support a data format 3, such as Java programs and Python programs need to exchange data, then you have to use JSON "" #json序列化 (JSON and pickle operation basically consistent) dicts = {' name ': ' Tantianran ', ' Age ': 25}res = json.dumps (dicts) print res# JSON serialization is saved to the file Json.dump (Dicts,open (' Json_data.jo ', ' W ')) #将一个字典序列化后存储到文件里result = json.load (open ( ' Json_data.jo ', ' R ')) #反序列化成json形式print resultprint type (Result) #查看数据类型, which is the original dictionary data type
This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1870861
Serialization of Python (Pickle module and JSON module)