最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python用pickle模塊實(shí)現(xiàn)“增刪改查”的簡(jiǎn)易功能

 更新時(shí)間:2017年06月07日 11:49:32   作者:yangbin  
本篇文章主要介紹了python用pickle模塊實(shí)現(xiàn)“增刪改查”的簡(jiǎn)易功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

pickle的作用:

1:pickle.dump(dict,file)把字典轉(zhuǎn)為二進(jìn)制存入文件.

2:pickle.load(file)把文件二進(jìn)制內(nèi)容轉(zhuǎn)為字典

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()

運(yùn)行結(jié)果:

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#

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

青浦区| 调兵山市| 大石桥市| 宾阳县| 容城县| 鄂伦春自治旗| 色达县| 阿巴嘎旗| 贵南县| 额济纳旗| 汉源县| 阳城县| 东兰县| 苗栗市| 光泽县| 金溪县| 会泽县| 揭阳市| 哈密市| 新民市| 九江县| 宁德市| 来安县| 镇平县| 孙吴县| 淳安县| 石景山区| 当涂县| 吴江市| 阳高县| 绥化市| 屏东县| 文化| 建水县| 大竹县| 永定县| 柏乡县| 白沙| 红安县| 济宁市| 罗定市|