我正在使用带有openpyxl模块的Python 3.5。 我有一个小的Excel电子表格,它有5列,每个列有5个特定类型的电影。 我的Python程序旨在根据用户输入显示特定类型的电影,将电影添加到电子表格,删除电影或退出。 现在,我已经让它在用户选择的类别中显示电影,但是,我无法正确删除它。
它将显示该类型的电影列表,无论用户输入的是什么号码,它都会自动选择选项#5。我不知道怎么让它正确匹配。
更糟糕的是,如果选择删除(通过输入' Y'或' N'),有时它仍然会删除。
而且,即使excel工作簿应该" save" - 重新启动程序后,删除的文件将再次显示。
我一直在看这段代码好几个小时,我不知道该改变什么。 你们可以看看并给我一些建议吗?
我知道您无法访问电子表格,但这里不是电子表格布局的图片:
以下是从头开始的代码:
import openpyxl
wb = openpyxl.load_workbook("Movies.xlsx")
sheet = wb.active
sheetname = wb.get_sheet_by_name("Movies")
thrillers = list(sheet['A2' : 'A6'])
comedies = list(sheet['B2' : 'B6'])
action = list(sheet['C2' : 'C6'])
dramas = list(sheet['D2' : 'D6'])
family = list(sheet['E2' : 'E6'])
def greeting():
print("\tWELCOME TO YOUR MOVIE DATABASE!\n")
print("Please select an option from the menu:\n")
def menu_selection():
print("\n")
print("\t ** MENU **")
print("1. Search the movie database\n" \
"2. Add a movie to the database\n" \
"3. Delete a movie from the database\n" \
"4. Exit the program\n")
以下是该功能的代码,根据用户输入的类型显示数据库中的电影:
def movie_selector():
print("1. Thriller\n" \
"2. Comedy\n" \
"3. Action\n" \
"4. Drama\n" \
"5. Family\n" \
"6. Exit to main menu")
print("\n")
selection = int(input("Enter your selection: "))
print("\n")
if selection == 1:
print("\t ** " + sheet['A1'].value + " **")
for m in thrillers:
print(m[0].value)
elif selection == 2:
print("\t ** " + sheet['B1'].value + " **")
for m in comedies:
print(m[0].value)
elif selection == 3:
print("\t ** " + sheet['C1'].value + " **")
for m in action:
print(m[0].value)
elif selection == 4:
print("\t ** " + sheet['D1'].value + " **")
for m in dramas:
print(m[0].value)
elif selection == 5:
print("\t ** " + sheet['E1'].value + " **")
for m in family:
print(m[0].value)
elif selection == 6:
menu_selection()
else:
print("Invalid selection. Try again.")
movie_selector()
以下是删除电影的功能,没有任何效果:
def delete_movies():
print("\t ** CATEGORIES **\n")
print("1. Thriller\n" \
"2. Comedy\n" \
"3. Action\n" \
"4. Drama\n" \
"5. Family\n" \
"6. Exit to main menu")
selection = int(input("\nSelect the category of the movie you want to delete: "))
n = 0
print("\n")
if selection == 1:
print("\t ** " + sheet['A1'].value + " **")
for m in thrillers:
n += 1
print(str(n) + '. ' + m[0].value)
elif selection == 2:
print("\t ** " + sheet['B1'].value + " **")
for m in comedies:
n += 1
print(str(n) + '. ' + m[0].value)
elif selection == 3:
print("\t ** " + sheet['C1'].value + " **")
for m in action:
n += 1
print(str(n) + '. ' + m[0].value)
elif selection == 4:
print("\t ** " + sheet['D1'].value + " **")
for m in dramas:
n += 1
print(str(n) + '. ' + m[0].value)
elif selection == 5:
print("\t ** " + sheet['E1'].value + " **")
for m in family:
n += 1
print(str(n) + '. ' + m[0].value)
elif selection == 6:
menu_selection()
else:
print("Invalid selection. Try again.")
movie_selector()
delete_num = int(input("\nEnter the number of the movie to delete: "))
if delete_num == 1:
confirm = input("Delete " + m[0].value + " (Y or N)? ") # it outputs #5 value no matter what
if confirm == 'y' or 'Y':
print("Ok, deleted.")
m[0].value = ""
wb.save("Movies.xlsx")
elif confirm == 'n' or 'N':
print("Not deleted")
elif delete_num == 2:
confirm = input("Delete " + m[0].value + " (Y or N)? ")
if confirm == 'y' or 'Y':
print("Ok, deleted.")
m[0].value = ""
wb.save("Movies.xlsx")
elif confirm == 'n' or 'N':
print("Not deleted.")
elif delete_num == 3:
confirm = input("Delete " + m[0].value + " (Y or N)? ")
if confirm == 'y' or 'Y':
print("Ok, deleted.")
m[0].value = ""
wb.save("Movies.xlsx")
elif confirm == 'n' or 'N':
print("Not deleted.")
elif delete_num == 4:
confirm = input("Delete " + m[0].value + " (Y or N)? ")
if confirm == 'y' or 'Y':
print("Ok, deleted.")
m[0].value = ""
wb.save("Movies.xlsx")
elif confirm == 'n' or 'N':
print("Not deleted.")
elif delete_num == 5:
confirm = input("Delete " + m[0].value + " (Y or N)? ")
if confirm == 'y' or 'Y':
print("Ok, deleted.")
m[0].value = ""
wb.save("Movies.xlsx")
elif confirm == 'n' or 'N':
print("Not deleted.")
else:
print("Invalid selection. Try again.")
我希望有人可以告诉我出了什么问题。我将非常感激。
答案 0 :(得分:2)
在删除中,您正在修改M:
m[0].value = ""
然而,M只是程序中的值。您需要修改工作表的单元格:
sheetname['A2'] = ""
您可以建立单元格字符串(' A2',类似于' A' + str(i))以删除正确的单元格
答案 1 :(得分:0)
点击此if delete_num == 1:
confirm = input("Delete " + m[0].value + " (Y or N)? ") # it outputs #5 value no matter what
您正在尝试访问已在IF语句中声明的变量。您应该在函数开始之后立即声明它
def delete_movies():
m = ""
print("\t ** CATEGORIES **\n")
print("1. Thriller\n"
"2. Comedy\n"
"3. Action\n"
"4. Drama\n"
"5. Family\n"
"6. Exit to main menu")