26. JSON Learning in Python

Source: Internet
Author: User

1.JSON Sequence Description: Provides 4 keywords: dumps,dump,loads,load (exactly the same as the pickle usage)

Syntax: F.write (bytes (Json.dumps (dict), encoding= "Utf-8"))

R=json.loads (F.read (). Decode ("Utf-8"))

The dictionary is stored in a file because the dictionary format cannot be converted into byte format, so the JSON is introduced

A. Converting a dictionary into a JSON sequence into a file

 
Import JSON

accounts={
1000:{
' name ': ' Tom ',
' email ': ' [email protected] ',
' passwd ': ' abc123 ',
' balance ': 20000,
' phone ': ' 44165465 ',
' Bank_acc ': {
' ICBC ': 165116161,
' CBC ': 16516516516,
' ABC ': 656151616516
}
},
100 1:{
' name ': ' Tom1 ',
' email ': ' [email protected] ',
' passwd ': ' abc1123 ',
' Bala nCE ': 210000,
' phone ': ' 441654165 ',
' Bank_acc ': {
' ICBC ': 16511161611,
' CB C ': 165165165161,
' ABC ': 6561516165161
}
}
}

Print (Json.dumps (accounts)) #将字典转 into a JSON sequence
F=open ("Account.db", "WB") #写入文件account. db
F.write (bytes (json.dumps (accounts), encoding= "Utf-8")) # Write to File account.db
F.close ()
-------------------------------------------------------------------
{"+": {"name": "Tom", "email": "[email protected]", "passwd": "abc123", "balance": 20000, "phone": "44165465", "Bank_ac C ": {" ICBC ": 165116161," CBC ": 16516516516," ABC ": 656151616516}}," 1001 ": {" name ":" Tom1 "," email ":" [email protected] ", "passwd": "abc1123", "balance": 210000, "Phone": "441654165", "Bank_acc": {"ICBC": 16511161611, "CBC": 165165165161, "ABC ": 6561516165161}}

B. Recreate a new Python file to read the contents of the account.db:

 Import Pickle 

Account_file=open ("Account.db", "RB") #打开文件
Account_dict=json.loads (account_ File.read (). Decode ("Utf-8")) #以json加载文件
#print (account_dict)
Account_file.close ()
#print (Type (account_ dict['] [' balance ']))
account_dict[' + ' [' balance ']-= #取字典中balance然后减500
P Rint (account_dict)

F=open ("Account.db", "WB")
F.write (bytes (json.dumps), account_dict "encoding=" )) #将变更后的内容dumps入文件
F.close ()
Print (account_dict)
---------------------------------------------------- -------------------------

{' + ': {' name ': ' Tom ', ' email ': ' [email protected] ', ' passwd ': ' abc123 ', ' balance ': 20000, ' phone ': ' 44165465 ', ' Bank_ac C ': {' ICBC ': 165116161, ' CBC ': 16516516516, ' ABC ': 656151616516}}, ' 1001 ': {' name ': ' Tom1 ', ' email ': ' [email protected] ', ' passwd ': ' abc1123 ', ' balance ': 210000, ' phone ': ' 441654165 ', ' Bank_acc ': {' ICBC ': 16511161611, ' CBC ': 165165165161, ' ABC ': 6561516165161}}}
{' + ': {' name ': ' Tom ', ' email ': ' [email protected] ', ' passwd ': ' abc123 ', ' balance ': 19500, ' phone ': ' 44165465 ', ' Bank_ac C ': {' ICBC ': 165116161, ' CBC ': 16516516516, ' ABC ': 656151616516}}, ' 1001 ': {' name ': ' Tom1 ', ' email ': ' [email protected] ', ' passwd ': ' abc1123 ', ' balance ': 210000, ' phone ': ' 441654165 ', ' Bank_acc ': {' ICBC ': 16511161611, ' CBC ': 165165165161, ' ABC ': 6561516165161}}}
{' + ': {' name ': ' Tom ', ' email ': ' [email protected] ', ' passwd ': ' abc123 ', ' balance ': 19500, ' phone ': ' 44165465 ', ' Bank_ac C ': {' ICBC ': 165116161, ' CBC ': 16516516516, ' ABC ': 656151616516}}, ' 1001 ': {' name ': ' Tom1 ', ' email ': ' [email protected] ', ' passwd ': ' abc1123 ', ' balance ': 210000, ' phone ': ' 441654165 ', ' Bank_acc ': {' ICBC ': 16511161611, ' CBC ': 165165165161, ' ABC ': 6561516165161}}}

26. JSON Learning in 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.