我的代码中存在错误 - 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]
这是因为编辑中我的文件中有空行,解决这个问题的最简单方法是跳过这些行,我该怎么做?
答案 0 :(得分:1)
for循环中的一个简单条件可以解决问题。
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]