简体   繁体   English

当我的代码似乎在范围内时,为什么会出现 IndexError: list index out of range?

[英]Why am I getting an IndexError: list index out of range when my code appears to be within the range?

I was coding a project called "My Countdown Calendar".我正在编写一个名为“我的倒计时日历”的项目。 I was almost done and so I gave what I had so far a test run.我快完成了,所以我给了我到目前为止的测试运行。 I ended up getting this error.我最终得到了这个错误。 Here is my code.这是我的代码。

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)

When parsing a text file, you'll almost always want to skip empty lines.解析文本文件时,您几乎总是想跳过空行。 Here's an idomatic pattern for this:这是一个 idomatic 模式:

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

If your file format depends on whitespace on the non-empty lines, you can join lines 2 and 3 to if not line.strip() .如果您的文件格式取决于非空行上的空格,您可以将第 2 行和第 3 行连接到if not line.strip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在此代码中出现IndexError:list index超出范围? - Why am I getting IndexError: list index out of range in this code? 为什么我收到 IndexError: list index out of range 这个函数? - Why am I getting IndexError: list index out of range for this function? 为什么我收到 IndexError: list index out of range - Why am I getting IndexError: list index out of range 为什么在云上训练时会收到“IndexError: list index out of range”? - Why am I getting "IndexError: list index out of range" when training on the cloud? 我收到IndexError:列表索引超出范围 - I am getting IndexError: list index out of range 为什么收到此错误“ IndexError:字符串索引超出范围” - Why am I getting this Error “IndexError: string index out of range” 为什么会出现 IndexError:字符串索引超出范围? - Why am I getting an IndexError: string index out of range? 为什么我会收到此错误? IndexError:列表索引超出范围 - Why am I receiving this error? IndexError: list index out of range 为什么我得到 IndexError: list index out of range 错误,即使我有一个正确的列表 - Why am I getting IndexError: list index out of range error even though I have a proper list 为什么我收到此错误? twitter_list_dict.append(line [0])IndexError:列表索引超出范围 - why am I getting this error? twitter_list_dict.append(line[0]) IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM