#写程序最好是dump一次和load一次, you can save several files import jsoninfo={' name ': ' Tom ', ' age ': ' a ', ' job ': ' Work ',}f=open (' File1.txt ', ' W ') F.write (Json.dumps (info)) F.close ()
Put the dictionary into a file
info={' name ': ' Tom ', ' age ': ' A ', ' job ': ' Work ',}f=open (' Test.txt ', ' W ') f.write (str (info)) F.close ()
JSON reads the dictionary in reverse order
Import jsonf=open (' File1.txt ', ' R ') data=json.loads (F.read ()) F.close () print (data) print (data[' name '])
The function writes to the file with pickle, without JSON serialization
Import Pickle#pickle In this language there is python in Java that is not known pickledef func (type): #函数 print (' In the Func ', type) info = {' name ': ' Tom ' , ' age ': ' A ', ' job ': ' Work ', ' res ': func,}f=open (' File3.txt ', ' WB ') #f. Write (Pickle.dumps (info)) # Equal to Pickle.dump (info,f) f.close ()
function inverse sequence with pickle, without JSON serialization
Import pickledef func (type): #函数 print (' In the Func ', type) print (' content different ', type) #反序列的时候内容不一样f =open (' File3.txt ' RB ') #data =pickle.loads (F.read ()) This sentence is equal to the following data=pickle.load (f) f.close () print (data) print (data[' name ']) print ( data[' res '] (' string '))
Python: Serializing a dictionary into a file with JSON