Get the App
SLTechnology News&Howtos  ›  Network Security  › 

How to implement json Serialization by pickle

Shulou Source: shulou.com Published: 2022-05-31 23:19:29 09月30日 Update

This article mainly shows you "pickle how to achieve json serialization", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "pickle how to achieve json serialization" this article.

Serialization:

Convert an object to str or bytes

Deserialization:

Convert str or bytes to an object

Pickle is a python-specific serialization tool that serializes everything, including functions, classes, data types, etc., into bytes or binaries after serialization

Json is supported by all languages and only supports serialization of regular data types. After serialization, it is visual, the same as the original content, and more visible.

Pickle:

Dumps: returns a string or bytes (bytes) directly after serialization

Dump: written to file after serialization

When dumps and dump serialize the same content, the result is the same.

How to use it:

Import pickle

Note: json can also be serialized and deserialized, but json cannot handle complex data

a.

Serialize to file: pickle.dump (): serialize using this method

Example:

Import pickle

Dict1 = {'axiaqiaqizhuang 1thecontrolling nameplate VOLING LIUBUQUWeiTHANG LIUQUQUANG PROGRAPHY 25}

F = open ("test1.pkl", "wb")

Pickle.dump (dict1,f)

F.close ()

Note: the dictionary is written to disk at this time

If you are using json, use json.dump (dict1,f)

Lists, dictionaries, etc., can only be serialized before they can be stored in a file

Deserialization from file: pickle.load (): deserialization using this method

Example:

Import pickle

F = open ("test1.pkl", "rb")

Data = pickle.load (f)

Print (data)

F.close ()

Note: the file deserialization from dump to disk is displayed at this time.

If you are using json, use json.load (f)

b.

Do not serialize to file: pickle.dumps ()

Import pickle

Li = ['alex',11,22,'ok','sb']

Dumpsed = pickle.dumps (li)

Print (dumpsed)

Note: you can see that the li list has been serialized into binary content and cannot be viewed normally

Do not deserialize from the file: pickle.loads ()

Import pickle

Li = ['alex',11,22,'ok','sb']

Dumpsed = pickle.dumps (li)

Print (dumpsed)

Loadsed = pickle.loads (dumpsed)

Print (loadsed)

The serialization of json is the same as pickle:

Import json

Json can only serialize int,float,bool,str,list,dict,None

Dumps: returns strings, while pickle returns bytes

These are all the contents of the article "how pickle implements json serialization". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Sequences files content bytes data articles binaries dictionaries characters strings objects this method disk type visualization learning help support complexity things Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft MySQL Shulou Technology vpn