Python serialization and JSON

Source: Internet
Author: User

Python's serialization and deserialization is used for sharing between memory, including server and client sharing, sharing between two Python programs, and storing it as a string in a hard disk.

Pyhton's pickle can operate on Python's various data types, including classes, lists, objects, and so on. Pickle only applies to python.

Hard disk storage data can only be in the form of strings, which enables the interaction of memory data between two programs in this way

#Python的序列化和反序列化
Import pickle li = [' Xiaoli ', ' Xiaowang ', ' P ', ' 3 ', ' 4 ']dumped = Pickle.dumps (li)           #将列表进行了序列化print dumpedloaded = Pickle.loads (dumped)       #将序列化的数据进行了反序列化print loaded

stored in a file at the same time as serialized, and deserialized in a file

Import Pickleli = [' Xiaoli ', ' Xiaowang ', ' A ', ' 3 ', ' 4 ']pickle.dump (Li,open (' F:/python practice/temp.pk ', ' W '))      # After serializing the list to the file, print pickle.load (' F:/python practice/temp.pk ', ' r ') is stored   #将文件中的数据读取出来进行反序列化

Use of JSON

JSON can only manipulate simple data types, and it is not possible to manipulate complex data types such as classes. JSON is a common format for various languages.

The operation of JSON is almost the same as the pickle operation

Import Jsonli = [' Xiaoli ', ' Xiaowang ', ' xiaoming ', 12]print json.dumps (li)            #将列表序列化print json.loads (Json.dumps (LI))      #反序列化

Python serialization and JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.