当前脚本
import json
import sys
import re
import os
from loguru import logger
logger.add("customers.log", level="DEBUG", rotation="200 MB")
def is_valid_email(email):
pattern = r'^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
if not re.match(pattern, email):
print("输入邮箱格式不合规!")
return False
return True
def add_customer():
add_customer_name = input("请输入要添加的客户姓名:")
add_customer_age = input("请输入要添加的客户年龄:")
# 判断输入的年龄格式
if add_customer_age.isdigit():
while True:
add_customer_email = input("请输入要添加的客户邮箱:")
# 判断输入的邮箱地址
if is_valid_email(add_customer_email):
# 邮箱地址合规,跳出循环
break
else:
print("邮箱地址不合规,请重新输入!")
num = len(customers) + 1
new_customer = {num: {"name": add_customer_name, "age": add_customer_age, "email": add_customer_email}}
customers.update(new_customer)
save()
logger.info(f"新用户{add_customer_name}已添加")
print(f"新用户{add_customer_name}已添加")
print(customers)
else:
print("年龄输入无效,请输入数字")
def del_customer():
flag = False
del_customer_name = input("请输入要删除的客户编号:")
if del_customer_name in customers:
customers.pop(del_customer_name)
if customers:
save()
logger.info("删除成功")
print(f"已删除客户{del_customer_name}")
else:
save()
print("当前没有信息可以保存!")
else:
print("没有找到ID")
def update_customer():
# 方法1,根据用户名操作
# old_customer_name = input("请输入要修改的客户姓名:")
# for i in customers:
# if old_customer_name in customers.get(i).values():
# print(customers.get(i))
# update_customer_name = input("请输入新客户姓名:")
# update_customer_age = input("请输入新客户年龄:")
# # 判断old_customer_name输入的是否为数字
# if update_customer_age.isdigit():
# update_customer_email = input("请输入新客户邮箱:")
# ter = {
# i: {"name": update_customer_name, "age": update_customer_age, "email": update_customer_email}}
# customers.update(ter)
# else:
# print("输入不合格,重新输入")
# continue
# print(customers)
# 方法2,根据ID操作
updata_customer_ID = input("输入要修改的编号:")
if updata_customer_ID in customers:
updata_customer_name = input("输入要修改的姓名:")
updata_customer_old = input("输入要修改的年龄:")
updata_customer_email = input("输入要修改的邮箱:")
is_valid_email(updata_customer_email)
customers[updata_customer_ID] = {
"name": updata_customer_name,
"age": updata_customer_old,
"email": updata_customer_email
}
save()
logger.info("修改成功")
print(f"{updata_customer_ID}客户修改成功")
print("当前客户列表", customers)
else:
print(f"没有找到{updata_customer_ID}客户")
def query_one_customer():
# 检查customers变量是否存在且不为空
if 'customers' in globals() and customers:
ID = input("输入要查找的ID:")
if ID in customers:
customerD = customers[ID]
print(f"{ID}-姓名:{customerD['name']} 年龄:{customerD['age']} 邮箱:{customerD['email']}")
else:
print("Id不存在")
else:
print("当前没有客户信息!")
def query_all_customer():
if 'customers' in globals() and customers:
for i in customers:
print(f"{i}-姓名:{customers.get(i)['name']} 年龄:{customers.get(i)['age']} 邮箱:{customers.get(i)['email']}")
else:
print("当前没有信息!")
def quit_():
logger.info("退出")
sys.exit([0])
def check_and_write_file():
with open("bs.txt", "r+", encoding="utf8") as f: # 打开文件以读写模式
content = f.read() # 读取文件内容
if not content: # 判断文件内容是否为空
f.write("{}") # 如果为空,则写入{}
f.seek(0) # 将文件指针移回文件开头
print("文件为空,已写入{}")
def save():
new = json.dumps(customers)
with open("bs.txt", "w", encoding="utf8") as f:
f.write(new)
logger.info("保存成功")
print("保存完毕!")
def main():
global customers
logger.info("程序启动")
while True:
print("""
1.添加客户
2.删除客户
3.修改客户
4.查询一个客户
5.查询所有客户
6.退出
7.保存
""")
if not os.path.exists("bs.txt"):
with open("bs.txt", 'w',encoding="utf8") as file:
file.write("{}") # 可以写入一些初始内容,或者留空
print("文件'bs.txt'已创建。")
# 判断bs.txt文件是否为空,并在为空时写入{}
check_and_write_file()
with open("bs.txt", encoding="utf8") as f:
ter = f.read()
if ter:
customers = json.loads(ter)
sel_dist = {
1: add_customer,
2: del_customer,
3: update_customer,
4: query_one_customer,
5: query_all_customer,
6: quit_,
7: save,
}
choice = int(input("请选择:\n"))
func = sel_dist.get(choice)
if func:
func()
else:
print("无效选择!")
# 主程序调用
main()
主要实现了客户信息管理的功能,包括添加、删除、修改客户信息,以及查询单个或所有客户的信息。为了确保数据的一致性和持久性,系统使用JSON格式将数据存储到一个名为bs.txt的文件中。此外,系统还记录日志到customers.log文件中,方便追踪系统的操作历史。用户通过命令行接口与系统交互,可以选择不同的功能来管理客户信息。对于输入的数据,系统做了基本的验证工作,例如检查年龄是否为数字、邮箱格式是否正确等