其他分享
首页 > 其他分享> >

作者:互联网

一. 什么是 shelve 模块

二.使用方法

三.使用示例

1.保存数据

import shelve,datetime

f = shelve.open(r"./ttt.txt")
f["user_info"]  = {"name":"shawn","age":18}
f["hobby_list"] = ["read","sleep","run","eat"]
f["time_now"] = datetime.datetime.now()

f.close()

2.获取数据 (演示多种取值方法)

import shelve

f = shelve.open(r"./ttt.txt")

print(f.get("user_info"))  # {'name': 'shawn', 'age': 18}
print(f.get("hobby_list")) # ['read', 'sleep', 'run', 'eat']
print(f.get("time_now"))   # 2020-12-21 12:18:42.300267

print(f["user_info"])      # {'name': 'shawn', 'age': 18}
print(f["hobby_list"])     # ['read', 'sleep', 'run', 'eat']
print(f["time_now"])       # 2020-12-21 12:18:42.300267

print(f["user_info"]["name"])     # shawn
print(f["hobby_list"][1])         # sleep
print(f.get("user_info")["age"])  # 18
print(f.get("hobby_list")[0])     # read

f.close()

标签:get,18,user,模块,shelve,print,hobby
来源: https://www.cnblogs.com/songhaixing/p/14167301.html