python用pickle模块实现“增删改查”的简易功能
作者:yangbin 时间:2022-01-13 06:38:56
pickle的作用:
1:pickle.dump(dict,file)把字典转为二进制存入文件.
2:pickle.load(file)把文件二进制内容转为字典
import pickle
# 增
def adds():
users = {"name":"yangbin", "age":22, "sex":"male"}
with open("red.txt", "wb") as f:
pickle.dump(users, f)
dic = {}
with open("red.txt") as sd:
dic = pickle.load(sd)
print dic
# 删
def deletes():
dic = {}
with open("red.txt") as f:
dic = pickle.load(f)
dic.pop("sex")
with open("red.txt", "wb") as ff:
pickle.dump(dic, ff)
print dic
# 改
def changes():
dic = {}
with open("red.txt") as f:
dic = pickle.load(f)
dic["age"] = 28
with open("red.txt", "wb") as f:
pickle.dump(dic, f)
print dic
# 查
def finds():
dic = {}
with open("red.txt") as f:
dic = pickle.load(f)
for k,v in dic.items():
print "%s ---> %s" % (k, v)
adds()
deletes()
changes()
finds()
运行结果:
root@python3:/python/python2/linshi# python 01.py
{'age': 22, 'name': 'yangbin', 'sex': 'male'}
{'age': 22, 'name': 'yangbin'}
{'age': 28, 'name': 'yangbin'}
age ---> 28
name ---> yangbin
root@python3:/python/python2/linshi#
来源:http://www.cnblogs.com/yangbin1110/p/6938429.html?utm_source=tuicool&utm_medium=referral
标签:python,pickle模块
0
投稿
猜你喜欢
Matplotlib绘制子图的常见几种方法
2022-04-04 06:19:47
Python定时任务工具之APScheduler使用方式
2022-02-02 05:50:51
Python实现自定义Jupyter魔法命令
2022-03-10 11:47:04
解决Django生产环境无法加载静态文件问题的解决
2022-03-12 18:31:09
大大改善用户体验的10个小细节
2008-09-17 13:27:00
Jquery AJAX POST与GET之间的区别
2024-04-29 13:58:32
Python NumPy教程之二元计算详解
2023-12-16 18:25:04
Python+pyplot绘制带文本标注的柱状图方法
2023-06-26 16:09:11
表单验证中时间起止判断的递归处理
2009-12-16 19:27:00
如何使用Python调整图像大小
2021-11-30 00:10:14
ajax代理程序,自动判断字符编码
2007-11-04 13:17:00
pyqt5 实现 下拉菜单 + 打开文件的示例代码
2023-12-17 09:20:28
全透视:CSS Z-index 属性
2009-09-21 12:52:00
Element-ui el-tree新增和删除节点后如何刷新tree的实例
2023-07-02 17:03:52
vue和iview实现Scroll 数据无限滚动功能
2024-04-28 09:22:23
深入浅析SQL Server 触发器
2024-01-19 05:23:55
SQL学习笔记八 索引,表连接,子查询,ROW_NUMBER
2024-01-14 02:36:14
Python抓取移动App数据使用mitmweb监听请求与响应
2022-03-24 21:09:29
SqlServer 数据库 三大 范式
2024-01-15 06:58:55
Mysql主从复制注意事项的讲解
2024-01-28 10:57:32