python dictionary and study notes serialization and de-serialization

Dictionary of Learning:

# - * - Coding: UTF-. 8 - * - 
# a dictionary definition, there are a variety of data
dict1 = { 'name': ' zhangsan', 'age': '20', 'date': { 'id': '. 5', 'name': 'Zhang'}}
# print out the dictionary age value argument
Print (dict1 [ 'age'])
# print out the id value the value of the dictionary data corresponding to the
print (dict1 [ 'date' ] [ 'ID'])
# print out the dictionary key value
Print (dict1.keys ())
# print out the value of the dictionary valuez
print(dict1.values())


Remove the key value corresponding to the cycle value
= {dict2 'name': 'zhangsan', 'Age': '20 is'} 
for Key, dict2.items value in ():
Print (Key + ":" + value)

serialization and serialization return

 Serialization: it can be understood as a switch to a python object coding format string json

   ------ sequence of the python is built-in data types (list list, tuples tuple, dict Dictionary) is converted into str

Serialization back: can be understood as json format string decoded data object python

   ----- it is to serialize back into python str built-in data types (list list, dictionary dict)

Str JSON format string format -----

Json format string str There are various only one, text, html, xml

Serialized dump, dumps, return sequence of the Load, loads 
dump, the Load is serialized to a file and returns a sequence of
dumps, loads is serialized and non-return of the serialized file

Dictionary serialization and return serialization
import json

Dictionary sequence and back-serialization 
dict1 = { 'name': 'zhangsan', 'Age': '20 is'}
# previous dictionary unserialized content and data type
print (' dictionary unserialized content before { 0}, the data type: {1} \ n'.format (dict1 , type (dict1)))
after the # and serialize the contents of the dictionary type
dict_str = json.dumps (dict1)
Print after the contents ( 'dictionaries {serialized 0}}. 1 type {\ n'.format (dict_str, type ((dict_str))))
# of dictionary deserialized data type and content review
dict2 = json.loads (dict_str)
Print ( 'string dict_str back contents serialized: {0}, type {1} 'format (dict2, type (dict2)))
Serialization and de-serialization list
Serialization and deserialization list # 
list1 = [1,2,3,4,5]
SUMMARY print ( 'non-serialized before list {0}, the data type: {1} \ n'.format (list1 , type (List1)))
# after treatment sequence list and view the contents of data type
list_str = json.dumps (List1)
Print ( 'content list subsequent to a sequence of {0}, the data type: {1} \ n'. format (list_str, type (list_str) ))
after the contents list # deserialize data types and
List2 = json.loads (list_str)
Print ( 'list of contents after deserialization {0}, the data type: {1} \ n'.format (list2, type (list2) ))
 
Tuples serialization and deserialization
# tuples serialization and deserialization
tuple1 = (1,2,3,4,5)
content before ( 'tuple unserialized print {0}, Data Type: . 1} {\ n'.format (tuple1, type (tuple1)))
# tuples after serialization, and view the contents of data type
tuple_str = json.dumps (tuple1)
content after print ( 'sequence of tuples of {0 }, data type: {}. 1 \ n'.format (tuple_str, type (tuple_str)))
# tuples after deserialization, and view the contents of data type
Tuple2 = json.loads (tuple_str)
Print ( 'anti-tuple sequences after the contents of {0}, the data type: {1} \ n'.format (tuple2 , type (tuple2)))


Guess you like

Origin www.cnblogs.com/block-bky/p/12497571.html