sys,time,pickle,json

import sys
import time
# print(sys.argv)#命令行参数,第一个是路径
# print(sys.path)
# # if sys.argv[1] == "1":
# # print(1)
# # choice = input("wanna quit?")
# # if choice == "y" or choice == "Y":
# # exit("goodbye")#退出
# # sys.exit()#等同
# print(sys.version)#获取python版本
# print(sys.platform)#返回操作系统名称
# for i in range(101):
# sys.stdout.write(" ") # 每一次清空原行
# sys.stdout.write("%s%% | %s" % (int(i), int(i)*"#"))
# sys.stdout.flush() # 强制刷新到屏幕
# #print(str(i)+"%")
# time.sleep(0.05)
accounts = {
1000:{
"name":"tom",
"email":"mm.cc",
"passwd":"123",
"balance":12000,
"phone":"133415",
"bank_acc":{
"abcc":441
}
},
1001:{
"name":"jim",
"email":"dmm.cc",
"passwd":"1234",
"balance":10000,
"phone":"1233415",
"bank_acc":{
"abcc":554
}

}
}
import pickle
with open("h1.txt","wb") as f:
f.write(pickle.dumps(accounts))
with open("h1.txt","rb") as f2:
dic = pickle.loads(f2.read())
dic[1000]["balance"] -= 2000
with open("h1.txt", "wb") as f:
f.write(pickle.dumps(dic))
with open("h1.txt", "rb") as f2:
print(pickle.loads(f2.read()))
####等于上面
# with open("h1.txt","wb") as f:
# pickle.dump(accounts,f)
# with open("h1.txt","rb") as f2:
# dic = pickle.load(f2)
# dic[1000]["balance"] -= 2000
# with open("h1.txt", "wb") as f:
# pickle.dump(dic,f)
# with open("h1.txt", "rb") as f2:
# print(pickle.load(f2))
####
#import json
#json 用法同pickle,json局限但通用
#json.loads()用于将字典、列表、元组等形式的字符串,转换成相应的字典、列表、元组
#loads字符串必须里面都是双引号,例如:'{"n1":1}'
# with open("h1.txt","w") as f:
# f.write(json.dumps(accounts))
# with open("h1.txt","r") as f2:
# dic = json.load(f2)
# dic["1000"]["balance"] -= 3000
# with open("h1.txt", "w") as f:
# json.dump(dic,f)
# with open("h1.txt", "rb") as f2:
# print(json.load(f2))

原文地址:https://www.cnblogs.com/currynashinians000/p/8627172.html