IndexError:列表索引超出范围 - CSV文件 [英] IndexError: list index out of range - CSV file

查看:2152
本文介绍了IndexError:列表索引超出范围 - CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有一个错误 - IndexError:list index超出范围,在率[row [0]] = row [1]

There is an error in my code - IndexError: list index out of range, at rates[row[0]] = row[1]:

def change():
    # read file into dictionary
    with open('exchangeRate.csv', 'r') as in_file:
        echRdr = csv.reader(in_file)
        for row in echRdr:
            rates[row[0]] = row[1]

这是因为我的文件中有空行,由于编辑和最简单的方法来解决这是让它跳过这些行,

it is because there are empty lines in my file due to editing and the easiest way to solve this is to make it skip these lines, how would i do that?

推荐答案

在for循环中的一个简单的条件可能会解决这个问题。

A simple condition in the for loop may solve the issue.

def change():
    # read file into dictionary
    with open('exchangeRate.csv', 'r') as in_file:
    echRdr = csv.reader(in_file)
    for row in echRdr:
        if len(row) <= 1:
            pass
        else:
            rates[row[0]] = row[1]

这篇关于IndexError:列表索引超出范围 - CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆