列表索引超出范围,python位于大型CSV

时间:2015-04-06 21:34:22

标签: python list csv

我正在尝试获取两个csv文件,它们在第1列中具有相同的ID号。让python查找匹配并将两列附加到电子表格中的匹配行1.让他们在列中查找匹配项然后将匹配的行添加到相同的ID。

spreadsheet 1
0001, beth, merda
0002, mike, john
0003, mary, smith

电子表格2

0001, url, owner
0003, url, owner
0002, url, owner

修正后

0001, beth, merda, url, owner
0002, mike, john, url, owner
0003, mary, smith, url, owner

目前我收到的错误是:

File "payments.py", line 10, in <module>
    if row[0] in first:  # row[0] = clearing solution id
IndexError: list index out of range

如果我跑     print(len(first) 它说190.所以我不知所措。

import csv

#create a dict from first csv, with clearing solution id as key
with open("WEEKLY DDS PAYMENT UPLOADS 3-30-15--4-3-15 (1).csv", "r") as f:
    first = {rows[0]: rows[1:] for rows in list(csv.reader(f))}

# compare second csv and append rank
with open("report1428344317924.csv", "r") as f:
    for row in csv.reader(f):
        if row[0] in first:  # row[0] = clearing solution id
            first[row[0]].append(row[1])  # row[1] = rank


# convert dict back to list
merged = [(k,) + tuple(v) for k, v in first.items()]

# write list to output csv
with open('output.csv', "w") as f:
    csv.writer(f).writerows(merged)

1 个答案:

答案 0 :(得分:2)

检查报告文件是否有空行。 csv.reader将在空行返回0长度序列,因此row[0]将超出范围。无论如何,你不会从IndexError获得first,它是字典而不是列表,因此缺少的元素会引发KeyError

如果需要,可以轻松跳过空行,只需要像if row and row[0] in first:那样简单。