python - 为什么在 Python 3 中索引字符串而不是切片时会出现 IndexError?

标签 python string list

我是编程新手,正在尝试使用 Python 3。我发现了一些处理 IndexError 的主题,但似乎没有一个主题可以帮助解决这种特定情况。

我编写了一个函数,它打开一个文本文件,一次读取一行,并将该行分割成单独的字符串,每个字符串附加到一个特定的列表(记录行中每个“列”一个列表) )。大多数切片是多个字符 [x:y],但有些切片是单个字符 [x]。

我收到了 IndexError: string index out of range 消息,但据我所知,事实并非如此。这是函数:

def read_recipe_file():
    recipe_id = []
    recipe_book = []
    recipe_name = []
    recipe_page = []
    ingred_1 = []
    ingred_1_qty = []
    ingred_2 = []
    ingred_2_qty = []
    ingred_3 = []
    ingred_3_qty = []

    f = open('recipe-file.txt', 'r')  # open the file 
    for line in f:
        # slice out each component of the record line and store it in the appropriate list
        recipe_id.append(line[0:3])
        recipe_name.append(line[3:23])
        recipe_book.append(line[23:43])
        recipe_page.append(line[43:46])
        ingred_1.append(line[46]) 
        ingred_1_qty.append(line[47:50])
        ingred_2.append(line[50]) 
        ingred_2_qty.append(line[51:54])
        ingred_3.append(line[54]) 
        ingred_3_qty.append(line[55:])
    f.close()
return recipe_id, recipe_name, recipe_book, recipe_page, ingred_1, ingred_1_qty, ingred_2, ingred_2_qty, ingred_3, \
       ingred_3_qty

这是回溯:

Traceback (most recent call last):
  File "recipe-test.py", line 84, in <module>
    recipe_id, recipe_book, recipe_name, recipe_page, ingred_1, ingred_1_qty, ingred_2, ingred_2_qty, ingred_3, ingred_3_qty = read_recipe_file()
  File "recipe-test.py", line 27, in read_recipe_file
    ingred_1.append(line[46])

调用相关函数的代码是:

print('To show list of recipes: 1')
print('To add a recipe: 2')
user_choice = input()
recipe_id, recipe_book, recipe_name, recipe_page, ingred_1, ingred_1_qty, ingred_2, ingred_2_qty, \
ingred_3, ingred_3_qty = read_recipe_file()

if int(user_choice) == 1:
    print_recipe_table(recipe_id, recipe_book, recipe_name, recipe_page, ingred_1, ingred_1_qty,
                    ingred_2, ingred_2_qty, ingred_3, ingred_3_qty)

elif int(user_choice) == 2:
    #code to add recipe

失败的行是这样的:

ingred_1.append(line[46])

我尝试读取的文本文件的每一行都有超过 46 个字符,因此我不明白为什么会出现越界错误(下面是示例行)。如果我将代码更改为:

ingred_1.append(line[46:])

要读取切片而不是特定字符,该行会正确执行,但程序会在该行失败:

ingred_2.append(line[50])

这让我认为它在某种程度上与从字符串中附加单个字符有关,而不是多个字符的切片。

这是我正在阅读的文本文件中的示例行:

001Cheese on Toast     Meals For Two       012120038005002

我可能应该补充一点,我很清楚这总体上并不是很好的代码 - 我通常可以通过很多方法改进该程序,但据我所知,该代码实际上应该可以工作。

最佳答案

如果文件中的某些行为空或至少很短,就会发生这种情况。文件末尾的杂散换行符是一个常见原因,因为它会出现一个额外的空行。调试此类情况的最佳方法是捕获异常,并调查失败的特定(几乎可以肯定不会是您复制的示例行) :

try:
    ingred_1.append(line[46])
except IndexError:
    print(line)
    print(len(line))

捕获此异常通常也是处理错误的正确方法:您已经检测到了异常情况,现在您可以考虑该怎么做。例如,您可以:

  • 继续,这将默默地跳过处理该行,
  • 记录一些内容,然后然后继续
  • 通过引发一个新的、更具话题性的异常来摆脱困境:例如raise ValueError("Line Too Short")

如果这代表输入文件存在需要修复的问题,无论是否继续,打印相关的内容几乎总是一个好主意。如果它是相对琐碎的事情,并且您知道不会在其余处理中导致连续错误,那么默默地继续是一个不错的选择。您可能希望通过尽早检测“完全空”情况来区分“太短”和“完全空”情况,例如在循环顶部执行此操作:

if not line:
    # Skip blank lines
    continue

并适当处理其他情况的错误。


将其更改为切片之所以有效,是因为字符串切片永远失败。如果切片中的两个索引都在字符串外部(同一方向),您将得到一个空字符串 - 例如:

>>> 'abc'[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> 'abc'[4:]
''
>>> 'abc'[4:7]
''

关于python - 为什么在 Python 3 中索引字符串而不是切片时会出现 IndexError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668027/

相关文章:

在 C 中将字符串转换为 struct tm

c - 字符串代替c中的int

sql - 将数据帧添加到 Spark 中的列表

python - 删除qt中的子布局?

python - 有效地从列表中删除重复项和相似元素

iphone - 可以使用 NSLocalizedString 作为字典中的键吗?

Python:迭代具有不同维数的列表,有通用的方法吗?

python - 如何删除 Pandas DataFrame 中的小数点

php - 类似于Django的runserver的PHP本地测试服务器

Python - 实例变量列表的最小值