我遇到了IndexError:当行比所选索引长时,列表索引超出范围

  • 本文关键字:索引 列表 范围 IndexError 遇到 python
  • 更新时间 :
  • 英文 :


我希望通过一个文件迭代到每行上的所选索引,但即使行上的变量比我选择的索引多,我也会收到这个错误。

for thing in lines:
item = thing.split(',')
**price = item[4]**
total_Stock_Value = total_Stock_Value + float(price)
print('Total Value of Books in Stock: £{:0,.2f}'.format(total_Stock_Value,'n'))

检查文件末尾是否有空换行符,这是发生这种情况的常见位置。出现错误是有原因的,所以一定有一行没有足够的逗号。

最新更新