JSON module
JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (typically used to improve network transfer rates).
JSON is made up of lists and dict, respectively, in Python.
One, Python type data and JSON data format convert each other
Pthon in the STR type to JSON to Unicode type, none to null,dict corresponding to object
Ii. Data encoding and decoding
1. Simple Type Data codec
The so-called simple type refers to the Python type that appears in the previous table.
Dumps: Serializing an Object
#coding: Utf-8import json# Simple Code ===========================================print json.dumps ([' foo ', {' Bar ': (' Baz ', None, 1.0, 2)}]) # ["foo", {"bar": ["Baz", NULL, 1.0, 2]}] #字典排序print json.dumps ({"C": 0, "B": 0, "a": 0}, Sort_keys=true) # {"A": 0, "B": 0, "C": 0} #自定义分隔符print json.dumps ([1,2,3,{' 4 ': 5, ' 6 ': 7}], Sort_keys=true, separators= (', ', ': ')) # [1,2,3,{] 4 ": 5," 6 ": 7}]print json.dumps ([1,2,3,{' 4 ': 5, ' 6 ': 7}], Sort_keys=true, separators= ('/', '-')) # [1/2/3/{" 4 " -5/" 6 " -7}]# Increases indentation, enhances readability, but indents space makes the data larger print json.dumps ({' 4 ': 5, ' 6 ': 7}, sort_keys=true,indent=2, separators= (', ', ': ')) # {# "4": 5 , # "6": 7#}# Another more useful dumps parameter is Skipkeys, which defaults to false. # The Dumps method stores the Dict object, the key must be the STR type, and if there are other types, a TypeError exception will be generated, and if this argument is set to true, the key will be ignored. data = {' A ': 1, ($): 123}print json.dumps (data,skipkeys=true) #{"a": 1}
Dump: Serializing and saving an object to a file
#将对象序列化并保存到文件obj = [' foo ', {' Bar ': (' Baz ', None, 1.0, 2)}]
With open (r "C:\json.txt", "w+") as F:
Json.dump (OBJ,F)
Loads: deserializing a serialized string
Import jsonobj = [' foo ', {' Bar ': (' Baz ', None, 1.0, 2)}]a= json.dumps (obj) print json.loads (a) # [u ' foo ', {u ' bar ': [u ' Baz ', None, 1.0, 2]}]
Load: Reads and deserializes a serialized string from a file
With open (r "C:\json.txt", "R") as F:print Json.load (f)
Iii. custom complex Data type codec
For example, when we encounter object datetime, or a custom class object such as JSON default unsupported data type, we need to customize the codec function. There are two ways to implement custom codecs.
1, method one: Custom codec function
#! /usr/bin/env python#-*-coding:utf-8-*-# __author__ = "TKQ" Import Datetime,jsondt = Datetime.datetime.now () def time2str (obj): #python to JSON if Isinstance (obj, datetime.datetime): json_str = {"datetime": Obj.strftime ("%y-% m-%d%x ")} return json_str return objdef str2time (json_obj): #json to Python if ' datetime ' in Json_obj: date_str,time_str = json_obj["datetime"].split (") date = [Int (x) for x in Date_str.split ('-')] time = [int (x) for x in Time_str.split (': ')] dt = Datetime.datetime (date[0],date[1], date[2], time[0],time[1], time[2]) Return dt Return json_obja = Json.dumps (dt,default=time2str) print a# {"datetime": "2016-10-27 17:38:31"}print Json.loads (a,object_hook=str2time) # 2016-10-27 17:38:31
2, method Two: Inherit Jsonencoder and Jsondecoder class, rewrite related method
#! /usr/bin/env python#-*-coding:utf-8-*-# __author__ = "TKQ" Import Datetime,jsondt = Datetime.datetime.now () dd = [dt,[1,2 , 3]]class Myencoder (JSON. Jsonencoder): def default (self,obj): #python to JSON if Isinstance (obj, datetime.datetime): JS On_str = {"datetime": Obj.strftime ("%y-%m-%d%x")} return Json_str return Objclass Mydecoder (JSON. Jsondecoder): def __init__ (self): JSON. Jsondecoder.__init__ (self, object_hook=self.str2time) def str2time (self,json_obj): #json to Python if "Da Tetime "in json_obj:date_str,time_str = json_obj[" datetime "].split (") date = [Int (x) as x in Dat E_str.split ('-')] time = [INT (x) for x in Time_str.split (': ')] dt = Datetime.datetime (date[0],date[1 ], date[2], time[0],time[1], time[2]) return dt return json_obj# a = Json.dumps (DT,DEFAULT=TIME2STR) a =m Yencoder (). Encode (dd) print a# [{"datetime": "2016-10-27 18:14:54"}, [1, 2, 3]]print Mydecoder (). Decode (a) # [Datetime.datetime (2016, 10, 27, 18, 14, 54), [1, 2, 3]
Pickle Module
Python's pickle module implements all data sequences and deserialization of Python. There is basically no big difference between functional usage and JSON modules, and the same approach is dumps/dump and loads/load. Cpickle is the C-language compiled version of the Pickle module relatively faster.
Unlike JSON, Pickle is not used for data transfer between multiple languages, it only acts as a method to persist Python objects or to transfer objects between Python programs, so it supports all Python data types.
Pickle the deserialized object is equivalent to the original object, similar to the deepcopy.
Serialization of Dumps/dump
From datetime import datetry: import cpickle as pickle #python 2except importerror as E: import pickle #p Ython 3src_dic = {"Date":d ate.today (), "oth":([1, "a"],none,true,false),}det_str = Pickle.dumps (src_dic) Print det_str# (dp1# s ' Date ' # p2# cdatetime# date# p3# (S ' \x07\xe0\n\x1b ' # trp4# SS ' oth ' # p5# (lp6# i1# as ' a ' # ani01# i00# tp7# s.with o Pen (r "C:\pickle.txt", "W") as F: Pickle.dump (SRC_DIC,F)
Loads/load deserialization
From datetime import datetry: import cpickle as pickle #python 2except importerror as E: import pickle #p Ython 3src_dic = {"Date":d ate.today (), "oth":([1, "a"],none,true,false),}det_str = Pickle.dumps (src_dic) with open (R "C : \pickle.txt "," R ") as F: print pickle.load (f) # {' Date ': Datetime.date (), ' oth ': ([1, ' a '], None, True, Fal SE)}
The difference between JSON and pickle modules
1. JSON can only handle basic data types. The pickle can handle all Python data types.
2. JSON is used for character conversion between various languages. Pickle is used for the persistence of Python program objects or for object network transmission between Python programs, but different versions of Python serialization may have differences.