【问题标题】:My list in python is for some reason being overwritten with [' ']我在 python 中的列表由于某种原因被 [' '] 覆盖
【发布时间】:2020-11-28 05:28:38
【问题描述】:

我有一个 python 程序,它从 csv 文件中读取生日列表并确定其中是否有今天。代码应该读取每一行,拆分输入,并将每个组件分配给适当的变量。这是代码:

# checks birthday list for any birthdays
        for entry in birthday_list:
            string = birthday_list.readline()
            string = string[:-1]
            info = string.split(',')
            print(info)
            name = info[0]
            birth_day = int(info[1])
            birth_month = int(info[2])
            birth_year = int(info[3])
            # sends message
            if (day == birth_day and month == birth_month):
                age = year - birth_year
                wish_string = 'Happy ' + str(age) + determine_ordinal(age) + ' birthday, ' + name + '!\nhttps://imgur.com/a/G3wEPyg'
                await channel.send(wish_string)
        birthday_list.close()

真正的输出有个人信息,但看起来是这样的:

['Ponkachu', '7', '8', '2020']
['']

我在将birth_day 分配给 int(info[1]) 的行中得到一个 IndexError

print(string) 打印“Ponkachu,7,8,2020”

如果我将 print(info) 更改为 print(info[1]),它会给我“7”,然后是 IndexError

如果我把它改成 print(len(info)),我会得到这个:

4

1

我完全不知道为什么会这样。信息怎么了?怎么突然变了?

编辑:我刚刚检查了 csv 文件。 “Ponkachu,7,8,2020”实际上是第二行,所以由于某种原因跳过了第一行。

编辑 2:原来 entry 被设置为第一行的输入。此代码按预期工作:

for entry in birthday_list:
            info = entry.split(',')
            info[3] = info[3][:-1]
            name = info[0]
            birth_day = int(info[1])
            birth_month = int(info[2])
            birth_year = int(info[3])
            # sends message
            if (day == birth_day and month == birth_month):
                age = year - birth_year
                wish_string = 'Happy ' + str(age) + determine_ordinal(age) + ' birthday, ' + name + '!\nhttps://imgur.com/a/G3wEPyg'
                await channel.send(wish_string)
        birthday_list.close()

【问题讨论】:

  • 检查您的输入是否有空格。
  • 文件的最后一行是空的也很有可能。这很常见,您应该使您的代码对此具有鲁棒性。 (另外,检查 python 的csv 模块)
  • @KarthikRadhakrishnan 我刚刚检查了 csv 文件。 “Ponkachu,7,8,2020”实际上是第二行,所以由于某种原因跳过了第一行。
  • 您的循环将第一行放入entry。然后将下一行放入string。对吗?
  • 用虚拟数据替换个人信息并共享csv和代码

标签: python list csv index-error


【解决方案1】:

您的“生日列表”是文件还是普通列表?如果它是一个文件,那么在读取行本身之前,您不能真正使用“for each”循环。我会选择:

list_of_lines = birthdaylist.readlines()
for each in list_of_lines:
    string = each.rstrip() //remove newline character
    rest of your code

确保使用带有 S 的 readlines 来获取列表中的整个文件,而不仅仅是一行

【讨论】:

  • 根本没有理由打电话给readlinesfor each in birthday_list 很好;是对readline附加 调用导致了问题。
【解决方案2】:

迭代已经从文件中读取了一行;调用 readline 得到 next 行(忽略混合迭代和 readline 由于迭代器的内部缓冲的危险)。

    for entry in birthday_list:
        info = entry.rstrip('\n').split(',')
        print(info)
        name = info[0]
        birth_day = int(info[1])
        birth_month = int(info[2])
        birth_year = int(info[3])
        # sends message
        if day == birth_day and month == birth_month:
            age = year - birth_year
            wish_string = f'Happy {age}{determine_ordinal(age)} birthday, {name}!\nhttps://imgur.com/a/G3wEPyg'
            await channel.send(wish_string)
    birthday_list.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多