国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python用pickle模塊實現“增刪改查”的簡易功能

2020-02-16 01:39:35
字體:
來源:轉載
供稿:網友

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 ---> 28name ---> yangbinroot@python3:/python/python2/linshi#

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长兴县| 高要市| 抚松县| 浮梁县| 南昌市| 江孜县| 三原县| 宁蒗| 鹰潭市| 岐山县| 兰州市| 茌平县| 合作市| 金阳县| 清镇市| 高密市| 五大连池市| 海丰县| 新丰县| 昌乐县| 成都市| 木兰县| 来宾市| 九龙城区| 昌邑市| 汶上县| 邵阳县| 信宜市| 阳西县| 新余市| 翼城县| 永安市| 阿巴嘎旗| 沭阳县| 桦南县| 邢台市| 虞城县| 南部县| 建水县| 永年县| 冀州市|