Python data serialization---JSON & pickle

Source: Internet
Author: User
Tags serialization

JSON & Pickle Data Serialization serialization: is a list, dictionary, and other data type transfer string into a text file
Inverse sequence: Is the string from the text read out after a method into a list, dictionary and other data types. For example, eval () JSON is common: only some simple data types can be processed:
Json:
Serialization and deserialization functions:
Case 1:
Import JSON
info = {
"Name": "Brace",
"Age": 22,
}
JSON_STR = json.dumps (info) #序列化为字符类型
Print (Type (JSON_STR), JSON_STR)
data = Json.loads (JSON_STR) #反序列化为字典类型
Print (type data, data)
Output:
<class ' str ' > {' name ': ' Brace ', ' age ': 22}
<class ' dict ' > {' name ': ' Brace ', ' age ': 22}

Case 2:
Import JSON
info = {
"Name": "Brace",
"Age": 22,
}

With open ("Data", "W") as FS1:
Json.dump (info, FS1) #可以直接序列化后执行文件存储;

With open ("Data", "R") as FS2:
data = Json.load (FS2) #可以直接执行文件读取后反序列化;
Print (data, data) output:
<class ' dict ' > {' name ': ' Brace ', ' age ': 22}


Case Listing 3:
Import JSON
A = {5:1,9:2,1:3,8:4,3:9}
With open ("Data.txt", "W") as FS1:
Fs1.write (Json.dumps (a)) #序列化存储
Fs1.flush ()

With open ("Data.txt", "R") as FS2:
data = Json.loads (Fs2.read ()) #读取后反序列化
Print (data)

{' 5 ': 1, ' 9 ': 2, ' 1 ': 3, ' 8 ': 4, ' 3 ': 9}


Pickle: can only be used in Python:
Can perform some slightly more complex serialization and deserialization

Case 1:
Import Pickle
def sayhi (name):
Print ("Hello%s"%name)

info = {
"Name": "Brace",
"Age": 22,
"Func": Sayhi
}

With open ("Data.txt", "WB") as FS1:
Fs1.write (Pickle.dumps (info)) #pickle. Dumps () The binary type of the conversion. So you have to use WB to store
Fs1.flush ()

With open ("Data.txt", "RB") as FS2: #pickle. Loads () deserialize after reading 2 of the binary data
data = Pickle.loads (Fs2.read ())
Print (data)

Output:
{' name ': ' Brace ', ' age ': $, ' func ': <function sayhi at 0x02f6ba08>}


Case 2:
Pickle Dump and Load method: Same as above function, but can direct file operation


Import Pickle
def sayhi (name):
Print ("Hello%s"%name)

info = {
"Name": "Brace",
"Age": 22,
"Func": Sayhi
}

With open ("Data.txt", "WB") as FS1:
Pickle.dump (info, FS1) #可以直接序列化后执行文件存储;

With open ("Data.txt", "RB") as FS2:
Data=pickle.load (FS2) #可以直接执行文件读取后反序列化;
Print (data)

Output:
{' name ': ' Brace ', ' age ': $, ' func ': <function sayhi at 0x02f6ba08>}

Python data serialization---JSON & pickle

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.