二十python中json学习

Posted csjin-study

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二十python中json学习相关的知识,希望对你有一定的参考价值。

1.json序列介绍:提供4个关键字:dumps,dump,loads,load(与pickle用法完全相同)

  语法:f.write(bytes(json.dumps(dict),encoding="utf-8"))

     r=json.loads(f.read().decode("utf-8"))

  将字典存入文件,由于字典格式不能转化成字节格式的,所以引入json

  A.将一个字典转化成json序列存入一个文件


import json

accounts={
1000:{
‘name‘:‘Tom‘,
‘email‘:‘[email protected]‘,
‘passwd‘:‘abc123‘,
‘balance‘:20000,
‘phone‘:‘44165465‘,
‘bank_acc‘:{
‘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
}
}
}

print (json.dumps(accounts)) #将字典转成json序列
f=open("account.db","wb") #写入文件account.db
f.write(bytes(json.dumps(accounts),encoding="utf-8")) #写入文件account.db
f.close()
-------------------------------------------------------------------
{"1000": {"name": "Tom", "email": "[email protected]", "passwd": "abc123", "balance": 20000, "phone": "44165465", "bank_acc": {"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.重新创建一个新的python文件读取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[‘1000‘][‘balance‘]))
account_dict[‘1000‘][‘balance‘] -= 500 #取字典中balance然后减500
print (account_dict)

f=open("account.db","wb")
f.write(bytes(json.dumps(account_dict),encoding="utf-8")) #将变更后的内容dumps入文件
f.close()
print (account_dict)
-----------------------------------------------------------------------------

{‘1000‘: {‘name‘: ‘Tom‘, ‘email‘: ‘[email protected]‘, ‘passwd‘: ‘abc123‘, ‘balance‘: 20000, ‘phone‘: ‘44165465‘, ‘bank_acc‘: {‘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}}}
{‘1000‘: {‘name‘: ‘Tom‘, ‘email‘: ‘[email protected]‘, ‘passwd‘: ‘abc123‘, ‘balance‘: 19500, ‘phone‘: ‘44165465‘, ‘bank_acc‘: {‘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}}}
{‘1000‘: {‘name‘: ‘Tom‘, ‘email‘: ‘[email protected]‘, ‘passwd‘: ‘abc123‘, ‘balance‘: 19500, ‘phone‘: ‘44165465‘, ‘bank_acc‘: {‘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}}}

 



















































以上是关于二十python中json学习的主要内容,如果未能解决你的问题,请参考以下文章

python学习(二十四)开发接口

python学习(二十六)网络编程

python 学习二十五天(python内置模块之序列化模块)

Python学习笔记第二十六周(Django补充)

Python机器学习(二十)决策树系列三—CART原理与代码实现

python学习手册:第二十二章——模块代码编写基础