我使用Python 3.5與openpyxl模塊。 我有一個小的Excel電子表格,它有5列,每個電影有5個特定類型的電影。 我的Python程序旨在基於用戶輸入,顯示某種流派的電影,向電子表格添加電影,刪除電影或退出。 現在,我已經得到它在用戶選擇的類別中顯示電影,但是,我無法正確刪除它。我的Python代碼有邏輯錯誤,但不知道如何修復
它將顯示流派中的電影列表,並且不管用戶輸入什麼數字,它都會自動選擇選項#5。我不知道如何讓它正確匹配。
更糟的是,當給出刪除選項時(通過輸入'Y'或'N'),有時它仍然會刪除。
而且,即使Excel工作簿應該「保存」 - 一旦重新啓動程序,刪除的文件將再次顯示。
我一直在看這個代碼幾個小時,我不知道要改變什麼。 你們可以請看看並給我一些建議嗎?
我知道你不能訪問電子表格,但這裏ISN電子表格佈局的圖像:
,這裏是從一開始的代碼:
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.")
我希望有人能告訴我什麼錯誤。我將不勝感激。
看來你並沒有直接操縱excel對象,只是在執行delete函數時對'list'對象進行了一些更改。 – Acepcs
感謝您的回覆。 我將如何直接操縱它? – Jess
我的建議是使用'xlrd'模塊,因爲它很容易學習,並且提供的操作可以很容易地執行。 – Acepcs