Python: Serializing a dictionary into a file with JSON

Source: Internet
Author: User

#写程序最好是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

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.