Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement json Serialization by pickle

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report