Python------JSON and pickle data serialization

Source: Internet
Author: User
Tags serialization

I. JSON serialization

XML is replaced by JSON, and the language conversion between different platforms can only be handled simply. complex use of pickle;

Pickle can only be used in Python, and JSON is also recognized in Java.

 info = {  "  Name   ": "   Xiaolaizi   " ,  "  age   ' : 22}f  = open ("  test.text   ", "   W   " ) f.write (str (info)) 
------------- -----------------------------------
Import JSON
info = {
' Name ': ' Xiaolaizi ',
' Age ': 22
}
f = open ("Test.text", "W")
F.write (Json.dumps (info))

---------------------------------------
Import Pickle
info = {
' Name ': ' Xiaomaizi ',
' Age ': 22
}
f = open ("Test.text", "WB")
F.write (Pickle.dumps (info))
#等价pickle. Dump (info,f)
f = open ("test.text","R"=  eval(f.read ()) F.close ()print(data['age')     #反序列化
-------------------------------------------
Import JSON
f = open ("Test.text", "R")
data = Json.loads (F.read ())
F.close ()
Print (data[' age ')

--------------------------------------------
Import Pickle
f = open ("Test.text","RB")
data = Pickle.loads (F.read ())
#data = Pickle.loads (f)

F.close ()
Print (data[' age ')

Python------JSON and pickle data serialization

Related Article

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.