存储数据
用模块json来存储数据 json存储 以代码形式存储
模块json让你能够将简单的Python数据结构转储到文件中
并在程序再次运行时加载该文件中的数据
你还可以使用json在Python程序之间分享数据。
使用json.dump()和json.load()
# 代码所有:cxw
# 开发时间:2021/11/13 22:06
import json #使用json 模块来存储数据
filename="numbers.json"
num=[1,2,3,4,5,6]
with open(filename,"w") as files:
json.dump(num,files)
#json.dump()接受两个实参:要存储的数据和用于存储的文件对象
with open(filename) as filesa:
numbers=json.load(filesa)
#json.load()将列表存储到内存中,相当于读取问价,filename.read()
print(numbers)
保存和读取用户生成的数据
# 代码所有:cxw
# 开发时间:2021/11/13 22:19
#还是使用dump 和load 方法
import json
username=input("请输入你的名字:")
filename="users.json"
with open(filename,"w") as files: #在使用json时会需要写操作 w
json.dump(username,files)
print("当你返回时,将会记住你 ",username) # 调用load()函数读取已经被存储的文件,usersname
with open(filename) as filess:
use=json.load(filess)
print(use)
#优化
'''
import json
files="wusersname.json"
try:
with open(files) as f_ob:
username = json.load(f_ob)
except FileNotFoundError:
uses=input("请输入你的名字")
with open(files,"w") as f_obj:
json.dump(uses,f_obj)
print("we ",uses)
else:
print(username)
'''
重构
将代码划分为一系列完成具体工作的函数,成为重构,重构让代码更清晰,更容易扩展
#划分为一个函数
import json
def greet():
filename="uses.json"
try:
with open(filename) as file_ob:
users=json(file_ob)
except FileNotFoundError:
username=input("请输入你的名字:")
with open(filename,"w") as file_obj:
json.dump(username,file_obj)
print("我们将会记住你,并且返回你 ",username)
else:
print(users)
#重构函数 greet()
# 代码所有:cxw
# 开发时间:2021/11/13 22:54
# import json
# def greet():
# filename="uses.json"
# try:
# with open(filename) as file_ob:
# users=json.load(file_ob)
# except FileNotFoundError:
# username=input("请输入你的名字:")
# with open(filename,"w") as file_obj:
# json.dump(username,file_obj)
# print("我们将会记住你,并且返回你 ",username)
# else:
# print(users)
# greet()
#重构
import json
def greet_stroged_users():
filenames = "uses.json"
try:
with open(filenames) as files_obj:
uses=json.load(files_obj)
except FileNotFoundError:
return None
else:
print(uses)
def greet_unstroged_users():
username=greet_stroged_users()
if username:
print("欢迎回来,",username)
else:
username=input("请输入你的名字:")
filename="uses.json"
with open(filename,"w") as files:
json.dump(username,files)
print("我们将会记住你,并且返回你,",username)
greet_unstroged_users()
实例1
# 代码所有:cxw
# 开发时间:2021/11/13 23:09
# import json
# files="names.json"
# name=input("请输入你的名字")
# with open(files,"w") as file_oj:
# json.dump(name,file_oj)
# with open(files) as file_obj:
# username=json.load(file_obj)
# print("我知道你最喜欢的数字是",username)
#改进版
# 代码所有:cxw
# 开发时间:2021/11/13 23:09
# import json
# files="names.json"
# name=input("请输入你的名字")
# with open(files,"w") as file_oj:
# json.dump(name,file_oj)
# with open(files) as file_obj:
# username=json.load(file_obj)
# print("我知道你最喜欢的数字是",username)
#改进版
import json
def num_already_greet():
try:
file = "nums.json"
with open(file) as file_ob:
likesnum = json.load(file_ob)
except FileNotFoundError:
return None
else:
return likesnum
def greet_no_num():
likenum=num_already_greet()
if likenum:
print("你喜欢的数字又出现了",likenum)
else:
likesnum=input("输入你喜欢的数字:")
file="nums.json"
with open(file,"w") as file_obj:
json.dump(likesnum,file_obj)
print("返回你喜欢的数字:",likesnum)
greet_no_num()
#改进版3
'''
# 代码所有:cxw
# 开发时间:2021/11/13 23:09
# import json
# files="names.json"
# name=input("请输入你的名字")
# with open(files,"w") as file_oj:
# json.dump(name,file_oj)
# with open(files) as file_obj:
# username=json.load(file_obj)
# print("我知道你最喜欢的数字是",username)
#改进版
import json
def num_already_greet():
try:
file = "666.json"
with open(file) as file_ob:
likesnum = json.load(file_ob)
except FileNotFoundError:
return None
else:
return likesnum
def greet_new_users():
filename="666.json"
new_num=input("请输入你喜欢的数字:")
with open(filename,"w") as files:
json.dump(new_num,files)
return new_num
def greet_no_num():
likenum=num_already_greet()
if likenum:
print("你喜欢的数字又出现了",likenum)
else:
likesnum=greet_new_users()
print("你喜欢的数字是 ",likesnum)
greet_no_num()
'''