使用Python语言完成图书类别管理功能的开发,要求该功能至少包括:输出图书类别管理主菜单、添加图书类别信息、删除图书类别信息、修改图书类别信息、显示所有图书类别信息。要求将图书类别存储到booktype.csv文件中,文件格式为Excel表格
时间: 2023-12-04 15:05:24 浏览: 83
好的,我明白您的问题了。基于您的要求,可以使用Python语言来完成图书类别管理的开发,以下是一个简单的示例程序,可以实现您所需的功能:
```python
import csv
# 输出图书类别管理主菜单
def print_menu():
print("="*30)
print("欢迎使用图书类别管理系统")
print("1. 添加图书类别信息")
print("2. 删除图书类别信息")
print("3. 修改图书类别信息")
print("4. 显示所有图书类别信息")
print("0. 退出系统")
print("="*30)
# 添加图书类别信息
def add_booktype():
with open("booktype.csv", "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
booktype_name = input("请输入图书类别名称:")
writer.writerow([booktype_name])
print("图书类别信息添加成功!")
# 删除图书类别信息
def del_booktype():
booktype_list = []
with open("booktype.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
booktype_list.append(row[0])
booktype_name = input("请输入要删除的图书类别名称:")
if booktype_name in booktype_list:
booktype_list.remove(booktype_name)
with open("booktype.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
for booktype in booktype_list:
writer.writerow([booktype])
print("图书类别信息删除成功!")
else:
print("图书类别信息不存在!")
# 修改图书类别信息
def modify_booktype():
booktype_list = []
with open("booktype.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
booktype_list.append(row[0])
booktype_name = input("请输入要修改的图书类别名称:")
if booktype_name in booktype_list:
new_booktype_name = input("请输入新的图书类别名称:")
with open("booktype.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
for booktype in booktype_list:
if booktype == booktype_name:
writer.writerow([new_booktype_name])
else:
writer.writerow([booktype])
print("图书类别信息修改成功!")
else:
print("图书类别信息不存在!")
# 显示所有图书类别信息
def show_booktype():
print("当前所有图书类别信息如下:")
with open("booktype.csv", "r", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
print(row[0])
# 主函数
def main():
while True:
print_menu()
choice = input("请输入您的选择:")
if choice == "1":
add_booktype()
elif choice == "2":
del_booktype()
elif choice == "3":
modify_booktype()
elif choice == "4":
show_booktype()
elif choice == "0":
print("谢谢使用!")
break
else:
print("输入错误,请重新输入!")
if __name__ == "__main__":
main()
```
该程序使用csv模块来读写booktype.csv文件,实现了添加、删除、修改和显示图书类别信息的功能。您可以根据自己的需要进行修改和扩展。
阅读全文