python basis of Chapter XIV ------ serialization module: pickle

Sequence module: pickle

Module must guide the corresponding packet

import pickle

Serialization: that is, do not understand

Deserialization: will look into converting understand can understand

dumps: an arbitrary sequence of objects into one bytes

Format: pickle.dumps (data)

the pickle Import 
List1 = [ 'boss', 'second child', 'youngest', 'fourth of']
RES = the pickle.dumps (List1)
Print (RES)
outputs:
b'\x80\x03]q\x00(X\x06\x00\x00\x00\xe8\x80\x81\xe5\xa4\xa7q\x01X\x06\x00\x00\x00\xe8\x80\x81\xe4\xba\x8cq\x02X\x06\x00\x00\x00\xe8\x80\x81\xe4\xb8\x89q\x03X\x06\x00\x00\x00\xe8\x80\x81\xe5\x9b\x9bq\x04e.'

loads: the original data bytes to deserialize

Import the pickle 
byte = B '\ X80 \ X03] Q \ xOO (X-\ X06 \ xOO \ xOO \ xOO \ xe8 \ X80 \ X81 \ xe5 \ Xa4 \ xa7q \ x01X \ X06 \ xOO \ xOO \ xOO \ xe8 \ X80 \ x81 \ xe4 \ xba \ x8cq \ x02X \ x06 \ x00 \ x00 \ x00 \ xe8 \ x80 \ x81 \ xe4 \ xb8 \ x89q \ x03X \ x06 \ x00 \ x00 \ x00 \ xe8 \ x80 \ x81 \ xe5 \ x9b . \ x9bq \ x04e '
RES The pickle.loads = (byte)
Print (RES)
outputs:
[' boss', 'second child', 'youngest', 'fourth of']

the dump (data, file objects io): after a file is written to the target sequence io object (wb mode with only)
the pickle Import 
dict1 = { 'boss':' Sun Peng ',' second child ':' SUN ',' youngest ':' Sun Zhicheng '}
with Open (.py '12', 'WB') AS F:
the pickle .dump (dict1, f)
load (the object file io): The file object deserialized into sequence data (mode only with rb)
import pickle
with open('12.py','rb')as f:
res=pickle.load(f)
print(res)


Guess you like

Origin www.cnblogs.com/szc-boke/p/11262542.html