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