python错误:列表索引超出范围

时间:2014-04-02 03:45:19

标签: python python-2.7 set

我一直收到错误:

line 39, in fetchLocations
locations.add(row[-1])
IndexError: list index out of range

以下代码

def fetchLocations(filename):
  locations = set()
  with open(filename, 'rU') as fileObject:
    csvFile = csv.reader(fileObject, delimiter = ',', quotechar = '"')
    for row in csvFile:
        #print row[-1]
        locations.add(row[-1])
  return locations

但是,如果我取消注释

print row[-1]

它工作正常。任何线索为什么会发生这种情况!?

0 个答案:

没有答案