JSON serialization for Python

Source: Internet
Author: User

  

    
info = {
"Name":"Roger",
"Age":12
}

Open ("test.txt","W")
F.write (str (info))
F.close ()

Open ("test.txt","R")
data = F.read ()
Print (data["name"])
F.close ()
Will error

Use eval ()
Open ("test.txt","R")
data = F.read ()
Print (eval (data) ["name"])
F.close ()
In the form of JSON
Import JSON
info = {' name ':' Roger ',' age ':12}
Print (Json.dumps (info))
Open ("test.txt","W")
F.write (Json.dumps (info))
Import JSON
Open ("test.txt","R")
data = Json.loads (F.read ())
Print (data["age"])
F.close ()
One more example: pic
 import Pickle 
def functest ():
pass
info = { ' name ': Roger ' , ' Age ': 12, print (Pickle.dumps (info))
F = open ( Span style= "COLOR: #6a8759" > "test.txt" , F.write (Pickle.dumps (info))
 import Pickle 
def functest ():
pass
F = open ( "test.txt" , "RB")
data = Pickle.loads (F.read ())
print (Data[ "Age"])
F.close ()
One more example.
Import Pickle
Functest ():
Pass
info = {' name ':' Roger ',' age ':+,' func ': Functest}
Print (Pickle.dumps (info))
Open ("test.txt","WB")
F.write (Pickle.dumps (info))
Import Pickle
Functest ():
Print ("test")
Open ("test.txt","RB")
data = Pickle.loads (F.read ())
Print (data["func"] ())
F.close ()

The above can also be written in this:
Import Pickle
DefFunctest ():
Pass
info = {' Name ':' Roger ',' Age ':12, ' func ': functest}

F = open ( "test.txt" ,< Span style= "COLOR: #6a8759" > "WB")

Pickle.dump (info,f)

F.close ()

import Pickle
def Functest ():
print ( "test")
F = open (" test.txt " "RB")
data = Pickle.load (f)
print (Data[ "func"] ())
F.close ()

remember: Dump once and load once.




JSON serialization for 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.