Python base (17) - Serialization

Fast mold pickle

pickle.dumps (): may be converted to the data type specific bytes Type

pickle.loads (): The type of conversion back bytes

>>> import pickle
>>> a = {'name':'xing','weight':'110'}
>>> a_dump = pickle.dumps(a)
>>> print(a_dump)            # a_dump可写入文件
b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x04\x00\x00\x00xingq\x02X\x06\x00\x00\x00weightq\x03X\x03\x00\x00\x00110q\x04u.'

>>> c= pickle.loads(a_dump)
>>> print(c)
{'name': 'xing', 'weight': '110'}

 pickle.dump (data, file): directly writing file data serialized

pickel.load (file): file deserialization FIFO

json module

json.dump (data, f): the sequence of writing, but dump file is generated str type rather than bytes Type

josn.load (file): only load once

json.dumps()

json.loads()

the difference

pickle: only supports Python

    It supports all data types in Python

json: supports all languages

    Only supports regular data types in Python

Guess you like

Origin www.cnblogs.com/lalaxing/p/11364773.html