python sequence of full-stack checkpoints --20-

Serialization Introduction

What?

Serialization: → data transfer mechanism string
deserialize: string data structure →

use:

Storing data
transmission over the Internet

 

Using the Module:

  • josn
  • pickle
  • shelve

json:

  Can be converted to numbers, strings, lists, dictionaries, ancestral
  common serialization format
  only a small part can be converted into a string data type by json

pickle:

  All data in the python, can be converted into a string
  content pickle serialized python can understand only
  deserialization dependent python code

shelve:

  Serialization handles
  using a handle convenient operation,

 

json

Use dumps and loads converted in memory:

D = dict ([(. 1, ' A ' ), (2, ' B ' ), (. 3, ' D ' )])
 Print (type (D), D)
 # <class' dict '> {. 1:' A ', 2:' B ',. 3:' D '} 
s_d = json.dumps (D)
 Print (type (s_d), s_d)
 # <class' STR'> { ". 1": "A", "2 ":" b "," 3 ":" d "} string type, the character in double quotes 
dic_d = json.loads (s_d)
 Print (type (dic_d), dic_d)
 # <class' dict '> {'. 1 ':' a ',' 2 ':' b ',' 3 ':' d '} into a key character, not the beginning of an integer

 

Using dump and load files written to and read from files in:

f = open('fff',mode='w',encoding='utf-8')
json.dump(d,f)
f.close()

f = open('fff',mode='r')
fd= json.load(f)
print("%s fd:%s)" %(type(fd),fd))
f.close()

d = {1:'这个',2:'哪个'}
f = open('fff', MODE = ' W ' , encoding = ' UTF-. 8 ' ) 
The json.dump (D, F, ensure_ascii = True)
 # contains Chinese, is written to the file contents hash 
# to true is written to the file is a character encoding 
# to false content file is readable suction 
f.close () 

F = Open ( ' FFF ' , MODE = ' R & lt ' , encoding = ' UTF-. 8 ' )
 #   reading, without character encoding, the system uses the default character load binary, may be error 
fd = the json.load (f)
 Print ( " % S fd:% S) " % (of the type (fd), fd)) 
f.close ()

 

json can not write many times, but the use of file operations and dumps, loads can be adapted to achieve

# JSON written only once, can not read a plurality of lines 
# to be written into a plurality of rows, can only use the file to write multiple lines of dumps, the read line by line, loads the conversion using 
L = [{ ' K1 ' : ' . 11 ' }, { ' K2 ' : ' 22 is ' }, { ' K3 ' : ' 33 is ' }] 
with Open ( ' multiple read .txt ' , MODE = ' W ' , encoding = ' UTF -8 ' ) AS F:
     for D in L: 
        str_dic =  JSON.dumps(d)
        F.write(str_dic+'\n')

d_list = []
with open('多次读写.txt',mode='r',encoding='utf-8') as f:
    for l in f:
        dic = json.loads(l.strip())
        d_list.append(dic)

print(d_list)

 

pikcle

Operation consistent with json, but pickle support multiple read and write, and all data structures

Import the pickle
 Import Time 
D = dict ([(. 1, ' A ' ), (2, ' B ' ), (. 3, ' D ' )]) 
dic_str = the pickle.dumps (D)
 Print (type (dic_str), dic_str )
 # <class' bytes'> B '\ X80 \ X03} Q \ xOO (K \ x01X \ X01 \ xOO \ xOO \ x00aq \ x01K \ x02X \ X01 \ xOO \ xOO \ x00bq \ x02K \ x03X \ X01 \ xOO \ xOO \ x00dq \ x03u. ' 
D2 = the pickle.loads (dic_str)
 Print (D2) 

# result of binary conversion is not pickle, so read and write modes must use rb 
# pickle this can be written and read out 
with Open ( ' pickle_file ' ,mode=' WB ' ) AS F: 
    the pickle.dump (D, F) 
    the pickle.dump (time.localtime (), F) 
# After storage, the file content is garbled 

with Open ( ' pickle_file ' , MODE = ' RB ' ) AS F : 
    D1 = the pickle.load (F) 
    D2 = the pickle.load (F)
 Print (type (D1), D1)
 Print (type (D2), D2)

 

shelve

Directly operated by the file handle

Print ( ' The shelve the begin: ' .center (50, ' - ' ))
 Import The shelve 
F = shelve.open ( ' shelve_file ' ) # directly file handle is operated, can be stored in the data 
F [ ' Time ' ] = Time .localtime () 
F [ ' name ' ] = [ ' JUSE ' , ' Tom ' , ' Lisan ' ] 
f.close () 

with shelve.open ( ' shelve_file ' ,writeback=True) AS S: 
    S1 = S [ ' Time ' ] # Key value does not exist given 
    S2 = S [ ' name ' ] 
    S [ ' name ' ] [. 1] = ' ALTER edit123 '  # if not writeback = True, of modify the list of files will not return to the 
Print (of the type (s1), s1)
 Print (of the type (s2), s2)

 

Guess you like

Origin www.cnblogs.com/zxw-xxcsl/p/12660915.html
Recommended