使用Python语言完成图书类别管理功能的开发,要求该功能至少包括:输出图书类别管理主菜单、添加图书类别信息、删除图书类别信息、修改图书类别信息、显示所有图书类别信息。要求将图书类别存储到booktype.csv文件中,
时间: 2023-12-04 09:04:44 浏览: 81
好的,这是一个关于图书类别管理的Python程序开发问题。以下是一个简单的实现:
```python
import csv
def print_menu():
print("图书类别管理系统")
print("1. 添加图书类别")
print("2. 删除图书类别")
print("3. 修改图书类别")
print("4. 显示所有图书类别")
print("0. 退出系统")
def add_book_type():
book_type = input("请输入图书类别:")
with open("booktype.csv", "a", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow([book_type])
print("添加成功!")
def delete_book_type():
book_type = input("请输入要删除的图书类别:")
with open("booktype.csv", "r") as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader if row[0] != book_type]
with open("booktype.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(rows)
print("删除成功!")
def modify_book_type():
old_book_type = input("请输入要修改的图书类别:")
new_book_type = input("请输入新的图书类别:")
with open("booktype.csv", "r") as csvfile:
reader = csv.reader(csvfile)
rows = [row if row[0] != old_book_type else [new_book_type] for row in reader]
with open("booktype.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(rows)
print("修改成功!")
def show_book_type():
with open("booktype.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row[0])
if __name__ == "__main__":
while True:
print_menu()
choice = input("请选择操作:")
if choice == "1":
add_book_type()
elif choice == "2":
delete_book_type()
elif choice == "3":
modify_book_type()
elif choice == "4":
show_book_type()
elif choice == "0":
break
else:
print("输入错误,请重新输入!")
```
这个程序实现了图书类别管理系统的基本功能,包括添加、删除、修改和显示所有图书类别信息。图书类别信息被存储到一个名为booktype.csv的CSV文件中。用户可以通过输入相应的选项来执行不同的操作。
阅读全文