Serialization and deserialization of python-

Source: Internet
Author: User
Tags string format true true

Serialization module
# Serialization method
# format Conversion
# Convert data in Python to str---serialization
# Data---Deserialization that can be converted to Python by str
JSON module
# JSON All languages are universal, and the data it can serialize is limited: dictionary lists and tuples
Import JSON
# Json.dumps () and Json.loads () are a pair
# Json.dump () and Json.load () are a pair
# json.dumps () #序列号 the "obj" data type into a JSON-formatted string
# ret = Json.dumps ({' K ':()})
# Print (repr (ret), type (ret)) #str () is the object "output in string format", heavy in the output, repr () is "What is the Display object", the expression. So the latter is often used in the debugger to print.
# Ret2 = json.loads (ret) #将包含str类型的JSON文档反序列化为一个python对象
# Print (repr (Ret2), type (Ret2))
# #json. Dump () #理解为两个动作, an action is to convert "obj" to a JSON-formatted string, and another action is to write a string to a file, that is, the file descriptor FP is the required parameter "" "
#f = open (' Json_file ', ' W ')#Json.dump ({' k ':()},f)#f.close ()#With open (' Json_file ') as F:#ret = json.load (f)#Print (Ret,type (ret))#ret = Json.dumps ((1,2,3,4))#print (ret)#s = json.loads (ret)#print (s)#a = {' name ': ' Tom ', ' Age ': +}#With open (' Test.json ', ' W ', encoding= ' utf-8 ') as F:#F.write (Json.dumps (a,indent=4)) #indent =4 is a a few lines in the file, the default is 0 rows#With open ("Test.json", "R", encoding= ' Utf-8 ') as F:#AA = Json.loads (F.read ()) #将包含str类型的JSON文档反序列化为一个python对象"" "#f.seek (0)#BB = json.load (f) #将一个包含JSON格式数据的可读文件饭序列化为一个python对象#print (AA) #{' name ': ' Tom ', ' Age ': +}#Print (BB) #{' name ': ' Tom ', ' Age ': +}
View Code
Pickle Module
It's python-specific.
# There are also dumps () loads ()
# Dump () load () Use the same method as JSON
# pickle can serialize any data type in Python, Python is not compatible with other languages, and the result is bytes
# with Pickle serialized data, deserialization must also be used pickle
# Summary:
# 1.json Serialization method: Dumps: No file operation dump: Serialization + Write File
# 2.json Deserialization method: Loads: No file operation load: Read file + deserialization
# 3. JSON module serialized data is more general
# picle Module serialized data only python available, but powerful, can sequence number function
# 4.json modules can serialize and deserialize data types see Python object (obj) and JSON object's corresponding relational table
# Python object (obj) JSON
# Dict Object
# list,tuple Array
# str String
# int, float number
# True True
# False False
# None NULL
# 5. Format Write file using indent = 4
Shelve module
# shelve is also a serialization tool that Python provides to us, which is simpler than pickle.
# shelve only gives us an open method, which is accessed using key, and is similar to the dictionary.
 Importshelve#f = shelve.open (' shelve_file ')#f[' key ' = {' int ': Ten, ' float ': 9.5, ' string ': ' Sample data '} #直接对文件句柄操作 to deposit data#f.close ()##f1 = Shelve.open (' shelve_file ')#existing = f1[' key ']#f1.close ()#Print (existing)#there is a limit to this module, which does not allow multiple applications to write to the same db at the same time. So when we know that our application is only read, we can let shelve open the db by read-only mode .#do not support multiple people at the same time to write, support multiple people at the same time to read, if only read, set flag= ' R 'Importshelve#f = shelve.open (' shelve_file ', flag = ' R ')#existing = f[' key ']#f.close ()#Print (existing)#since shelve does not record any modifications to the persisted object by default, we need to modify the default parameters at Shelve.open () or the object's modifications will not be saved. #under normal circumstances, shelve open file handle does not perceive the value of the modification, set writeback = True to save the modified content#F2 = shelve.open (' Shelve_file ', writeback=true)## Print (f2[' key ')#f2[' key ' [' new_value '] = ' This is not here before '#print (f2[' key ')#f2.close ()
View Code
# Big Summary:
# JSON: All languages are generic, can be converted to a limited number of data types, the importance of five stars * * * *
# pickle: Python only, capable of converting all data types when playing games.
# shelve: Python language only, capable of converting all data types, using methods like dictionaries




Serialization and deserialization of python-

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.