Using common module of pickle and json

json module 

Import json
 from datetime Import datetime 


json.dumps json.dump and common data types can be converted into the data string


 1 ), for the data type datetime, need to make some modifications 

Data = DateTime.Now () 

needs to be rewritten json in a class: 
class ComplexEncoder (json.JSONEncoder):
     DEF default (Self, O):
         IF the isinstance (O, datetime):
             return o.strftime ( ' % Y-M-% D%% H:% M:% S ' )
         the else :
             return json.JSONEncoder.default (Self, O) 

with Open ( ' JSON serialized ' ,' W ' , encoding = ' UTF-. 8 ' ) AS F: 
    f.write (json.dumps (Data, CLS = ComplexEncoder) + ' \ n- ' )


 2 ), for the Chinese during serialization, which means will default Unicode replaced, if necessary, can be retained as a Chinese character string 

A = ' Michale want to buy Shanghai Room ' 

with Open ( ' JSON serialized ' , ' W ' , encoding = ' UTF-. 8 ' ) aS F: 
    The json.dump (A, F, ensure_ascii = False) 
        f.write ( ' \ n- ' )


From the above: json.dumps () operation of a conventional data directly, using the write () function writes the data file. The json.dump while () added directly in two parameters which can write data to a file. 
That is: the former string write operation, which directly work with files. 


For json.load and json.loads, the two can only be of type string conversion dictionary to come out, if there are other types of file data directly error!

 

 

pickle module 

pickle module may convert any type of data bytes stored to the type of data file. 
Course, can also direct these types of data fetch for the previous type of restore. 

Import the pickle 
Li = [1,2,3,4,5,6, ' QWE ' , ' sister ' ] 

Data = the pickle.dumps (Li)
 Print (Data)
 # B '\ X80 \ X03] Q \ xOO (K \ x01K \ x02K \ x03K \ x04K \ x05K \ x06X \ X03 \ xOO \ xOO \ x00qweq \ x01X \ X06 \ xOO \ xOO \ xOO \ xe5 \ xa6 \ xb9 \ xe5 \ XAD \ x90q \ x02e. ' 
DATAl = the pickle. loads (Data)
 Print (DATAl, type (DATAl))
 # [. 1, 2,. 3,. 4,. 5,. 6, 'QWE', 'sister'] <class' List '> 

# file operation 
# with Open (' the pickle serialization ',' wb ') as f :     The pickle.dump # (Li, F) 
#      f.write (the pickle.dumps (Li)) 

with Open ( ' the pickle serialized ' , ' RB ' ) AS F:
     # RES = the pickle.load (F) 
    # Print (RES , type (res)) # [ 1, 2, 3, 4, 5, 6, 'qwe', ' sister'] <class 'List'> 
    RES = reached, f.read () 
    DATA2 = The pickle.loads (RES)
     Print (data2) 

because the pickle module python unique, although you can serialize any type of data, but 
when you view the data stored in the file is displayed garbled. It may be garbled and between different versions of python.

 

summary of the pickle and json: 


pickle bytes serialized object is the object, into a sequence json str, of course, may be transcoded to a byte type; 
pickle serialized results are not universal, generic json serialization,!

 

Guess you like

Origin www.cnblogs.com/changwenjun-666/p/11461256.html