2017-10-18 7 views
-3

私は "My Countdown Calendar"というプロジェクトをコーディングしていました。私はほとんど終わってしまったので、これまでのテストをしました。私はこのエラーが発生した。 ここに私のコードです。コードが範囲内にあるように見える場合、IndexError:listインデックスが範囲外になるのはなぜですか?

from tkinter import Tk, Canvas 
from datetime import date, datetime 

def gete(): 
    liste = [] 
    with open('events.txt') as file: 
     for line in file: 
      line = line.rstrip('\n') 
      currente = line.split(',') 

      eventd = datetime.strptime(currente[1], '%d/%m/%y').date() 
      currente[1] = eventd 
      liste.append(currente) 
     return liste 
def daysbd(date1, date2): 
    timeb = str(date1-date2) 
    numberofd = timeb.split(' ') 
    return numberofd[0] 

root = Tk() 

c = Canvas(root, width=800, height=800, bg='black') 
c.pack() 
c.create_text(100, 50, anchor='w', fill='orange', font = 'Arial 28 bold underline', text = 'My Countdown Calendar') 


events = gete() 
today = date.today() 

for event in events: 
    eventn = event[0] 
    daysu = daysbd(event[1], today) 
    display = 'It is %s days until %s ' % (daysu, eventn) 
    c.create_text(100, 100, anchor='w', fill='lightblue',\ 
        font = 'Arial 28 bold ', text = display) 
+0

エラーが発生しているのは、どの回線ですか? – Barmar

+0

エラーコード – 0TTT0

+1

を投稿してください。私の推測では、ファイルに '、'がないので、 'currente [1]'が範囲外です。 – Barmar

答えて

0

テキストファイルを解析するときは、ほとんどの場合、空の行をスキップする必要があります。ここでは、このためのidomaticパターンです:

for line in file: 
    line = line.strip() 
    if not line: 
     continue 
    # process line 

あなたのファイル形式が非空行に空白文字に依存する場合、あなたはif not line.strip()にライン2と3に参加することができます。

+0

あなたの回答はとても役に立ち、高く評価されています。 – Osprey

関連する問題