二十六、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':'22016311@qq.com',
'passwd':'abc123',
'balance':20000,
'phone':'44165465',
'bank_acc':{
'ICBC':165116161,
'CBC':16516516516,
'ABC':656151616516
}
},
1001:{
'name':'Tom1',
'email':'220116311@qq.com',
'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": "22016311@qq.com", "passwd": "abc123", "balance": 20000, "phone": "44165465", "bank_acc": {"ICBC": 165116161, "CBC": 16516516516, "ABC": 656151616516}}, "1001": {"name": "Tom1", "email": "220116311@qq.com", "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': '22016311@qq.com', 'passwd': 'abc123', 'balance': 20000, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, '1001': {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}}
{'1000': {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 19500, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, '1001': {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}}
{'1000': {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 19500, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, '1001': {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}}

 

posted @ 2018-08-02 10:25  旅行没有终点  阅读(151)  评论(0编辑  收藏  举报