2017-03-22 22 views
0

私は、異なるセルを入力してコードを特定の列に印刷しようとしています。だから私は人が5つの異なるタイプの映画を入力し、次にopenpyxlがExcelファイルを反復して5つの映画を探し、行全体を印刷するようにコードを設計しましたが、それはエラーを吐き出し続けます。それをOpenpyxl:AttributeError: 'Workbook'オブジェクトに 'cell'という属性がありません

出力修正する方法を知っている:

Traceback (most recent call last): 
File "C:/Users/Shay/PycharmProjects/untitled/Assignment ITN.py", line 39, in <module> 
    listcreator(movieID) 
File "C:/Users/Shay/PycharmProjects/untitled/Assignment ITN.py", line 28, in listcreator 
    if sheet.cell(row=r, column=c).value == movieOne: 
AttributeError: 'Workbook' object has no attribute 'cell' 

を、これは私のコードです:

import csv 
import openpyxl 
from openpyxl import load_workbook 

movieID = openpyxl.load_workbook(filename="C:/Users/Shay/Downloads/Movie IDs - Student Copy.xlsx") 
movie_ID = movieID.active 


movie_ID_List = [] 
movie_Ratings_list = [] 
mveavg = [] 

movieOne = input("Type first Movie\n") 
movieTwo = input("Type in second movie\n") 
movieThree = input("Type in third movie\n") 
movieFour = input("Type in fourth movie\n") 
movieFive = input("Type in fifth movie\n") 

movieOneList = [] 
movieTwoList = [] 
movieThreeList = [] 
movieFourList = [] 
movieFiveList = [] 

def listcreator(sheet): 
    for r in range(1, 27278): 
     for c in range(1,2): 
      if sheet.cell(row=r, column=c).value == movieOne: 
       movieOneList.append(sheet.cell(row=r, column=0)) 
      if sheet.cell(row=r, column=c).value == movieTwo: 
       movieTwoList.append(sheet.cell(row=r, column=0)) 
      if sheet.cell(row=r, column=c).value == movieThree: 
       movieThreeList.append(sheet.cell(row=r, column=0)) 
      if sheet.cell(row=r, column=c).value == movieFour: 
       movieFourList.append(sheet.cell(row=r, column=0)) 
      if sheet.cell(row=r, column=c).value == movieFive: 
       movieFiveList.append(sheet.cell(row=r, column=0)) 

listcreator(movieID) 

print(movieOneList) 
print(movieTwoList) 
print(movieThreeList) 
print(movieFourList) 
print(movieFiveList) 
movieOneList - (movieOneList[0]) 
movieTwoList - (movieTwoList[0]) 
movieThreeList - (movieThreeList[0]) 
movieFourList - (movieFourList[0]) 
movieFiveList - (movieFiveList[0]) 
print(movieOneList) 
print(movieTwoList) 
print(movieThreeList) 
print(movieFourList) 
print(movieFiveList) 

私はこの質問が本当に思える場合ので、私はごめんなさい、私はnoobのコーダだということにも言及する必要があります基本的ですが、助けが好きです

答えて

1

変数にはより良い名前を付ける必要があります。

listcreator関数は、Worksheetオブジェクトを受け取ります。しかし、それを呼び出すと、そのブックのアクティブシートであるmovie_IDではなく、上部に読み込まれるWorkbookオブジェクトのmovieIDが渡されます。

これらのオブジェクトをより適切なものと呼んでいれば、間違ったことをより簡単に見ることができました。

関連する問題