python- serialization and deserialization (loads, load, dumps, dump)


Sequencing module
# serialization method
# format conversion
# python in converting data into a sequence of str ---
# data can be converted into the python str deserialization ---
json module
# Json are common to all languages, it serialized data is limited: the dictionary lists and tuples 
Import json
# json.dumps () and json.loads () is a pair of
# json.dump () and json.load () is a pair of
# json.dumps () # sequence number "obj" data type is converted into a string of JSON format
# json.dumps RET = ({ 'K' :( l, 2,3)})
# Print ( repr (ret), type (ret )) #str () is acting object "output string format", focusing on the output; the repr () is the "display object is something", focuses on the expression. Therefore, the debugger is often used to print the latter.
# Ret2 = json.loads (ret) # will contain the type str deserialize JSON document a python objects
# print (repr (ret2), type (ret2))
# # Json.dump () # understood as the two actions, an action is to convert the "obj" JSON format string, and a string of action is to write to a file, that file descriptor fp parameter must be a "" "
# f = open('json_file','w')
# json.dump({'k':(1,2,3)},f)
# f.close()
# with open('json_file') as f:
#     ret = json.load(f)
#     print(ret,type(ret))

# ret = json.dumps((1,2,3,4))
# print(ret)
# s = json.loads(ret)
# print(s)

# a = {'name':'tom','age':23}
# with open('test.json','w',encoding='utf-8') as f:
#     f.write(json.dumps(a,indent=4))  #indent=4是a在文件中显示几行,默认是0行
# with open("test.json", "r", encoding='utf-8') as f:
#     aa = json.loads (f.read ()) # str type comprising JSON document object is deserialized into a python "" " 
#      f.seek (0) 
#      BB = the json.load (F) containing a # rice-readable file format of data sequence into a JSON python objects 
# Print (AA) # { 'name': 'Tom', 'Age':} 23 is 
# Print (BB) # { 'name': 'Tom', 'age': 23}

 

The pickle module 
is unique python
# is also dumps () loads ()
# the dump () Load () using exactly the same method and json
# pickle in python any data type can be serialized, not drink python proprietary language compatible with the other results is bytes
# pickle serialized data, must also be deserialized pickle
Summary #: 
# 1.json serialization methods: dumps: no file operations dump: the sequence of file write +
# 2.json deserialize Method: loads: No file operation load: reading documents deserialize +
# 3. json serialized data module more versatile
# picle module python serialized data is only available, but powerful, can be a function of the sequence number
# 4.json module can serialize and deserialize data type objects see python (obj) of the object json correspondence table
# python the object (obj) JSON
# dict Object
# List, tuple Array
# STR String
# int, a float Number
# True to true
# to false False
# None null
# 5. the formatted document written using indent = 4
shelve module
# Shelve python also provide us with a sequence of tools, it is easier than with some of pickle 
# shelve only provide us with a open approach is to use the key to access, use and similar dictionary
Import The shelve
 # F = shelve.open ( 'shelve_file') 
# F [ 'Key'] = { 'int': 10, 'a float': 9.5, 'String': 'the Sample Data' #} operate directly on the file handle, can be stored in the data 
# f.close () 
# 
# F1 = shelve.open ( 'shelve_file') 
# existing F1 = [ 'Key'] 
# f1.close () 
# Print (existing) 
# this module has a limit, it does not support multiple applications at the same time to write to the same DB. So when we know our application if the read-only operation, we can make shelve open DB by read-only 

# does not support more than one person at the same time writing, reading support more than one person at the same time, if only to read of it set flag = 'r' 
Import The shelve
 # F = shelve.open ( 'shelve_file', In Flag = 'R & lt') 
# existing F = [ 'Key'
# Due to shelve the default will not record any changes to be persistent object of the case, so we () when you need to modify the default parameters in shelve.open, otherwise modify the object will not be saved. 
# Under normal circumstances shelve open file handles can not modify the perception of value, you can set the writeback = True Save the modified content of the 
# F2 = shelve.open ( 'shelve_file', writeback = True) 
# # Print (F2 [ 'Key' ]) 
# F2 [ 'Key'] [ 'NEW_VALUE'] = 'Not here Wallpaper before the this WAS' 
# Print (F2 [ 'Key']) 
# f2.close ()

 

Large sum #: 
# json: All common language, limited energy conversion of data types, the importance of the five stars *****
# pickle: limited python, can convert all types of data will be used when playing games
# shelve : limited python language, can convert all types of data, using a method similar to the dictionary



forwards: https://www.cnblogs.com/dwenwen/p/7874815.html

Guess you like

Origin www.cnblogs.com/hemingwei/p/11579265.html