码迷,mamicode.com
首页 > 编程语言 > 详细

python shelve模块

时间:2019-02-17 23:44:46      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:lex   value   item   key   port   add   imp   inf   end   

 

#coding=utf-8
import shelve

f = shelve.open("shelve_test")
f[info] = "alex"
f["age"] = [1,34,5,6,33,44]
f["name"] = {"name":"alex","add":"sz"}
f.close()

f = shelve.open("shelve_test")
# 这里这个f我们就可以理解为是一个字典对象,所以shelve存储数据就按照字典的方式存储数据


print(f.get("info"))
print(f.get("age"))
print(f.get("name"))
print(f[info])
print(f.values())
print(f.items())
print(f.keys())

输出

alex
[1, 34, 5, 6, 33, 44]
{name: alex, add: sz}
alex
ValuesView(<shelve.DbfilenameShelf object at 0x000001A88D374208>)
ItemsView(<shelve.DbfilenameShelf object at 0x000001A88D374208>)
KeysView(<shelve.DbfilenameShelf object at 0x000001A88D374208>)

 

#coding=utf-8
import shelve
with shelve.open("test_shelve.db") as f:
    f["k1"] = {
        "name":"xiaoming1",
        "age":12,
        "address":"beijing1"
    }
    f["k2"] = {
        "name":"xiaoming2",
        "age":12,
        "address":"beijing2"
    }
    f["k3"] = {
        "name":"xiaoming3",
        "age":12,
        "address":"beijing3"
    }
    f["k4"] = ["小明1","小明2","小明3"]
with shelve.open("test_shelve.db") as f:
    print(f["k1"]["name"])
    print(f["k4"][0])

输出

xiaoming1
小明1

 

#coding=utf-8
import shelve
test_list=[1,2,3,4,5]
test_dict={"aaa":23,"bbb":33}
s="xiaoming"
with shelve.open("shelve.ini","wc") as f:
    f["k1"] = test_list
    f["k2"] = test_dict
    f["k3"] = s
 
with shelve.open("shelve.ini","rc") as k:
    print(k["k3"])
    print(k["k2"])
    print(k["k1"])

输出

xiaoming
{aaa: 23, bbb: 33}
[1, 2, 3, 4, 5]

 

#coding=utf-8
import shelve


list = [1, 2, 3]
she = shelve.open(test.dat)
she[d] = list
she[d].append(f)
print(she[d])


list = [1, 2, 3]
she = shelve.open(test.dat)
she[d] = list
temp = she[d]
temp.append(f)
she[d] = temp
print(she[d])

输出

[1, 2, 3]
[1, 2, 3, f]

 

python shelve模块

标签:lex   value   item   key   port   add   imp   inf   end   

原文地址:https://www.cnblogs.com/sea-stream/p/10393243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!