在python中排序时列出索引超出范围

时间:2015-12-25 12:32:30

标签: python sorting csv

我正在尝试将CS​​V文件读入列表,然后根据列表的前两列对其进行排序(首先是第一列,然后是第二列,如果第一列相同的话)。这就是我在做的事情:

def sortcsvfiles(inputfilename,outputfilename):
    list1=[]
    row1=[]
    with open(inputfilename,'rt') as csvfile1:
        reader=csv.reader(csvfile1)
        cnt=0       
        for row in reader:
            if cnt==0:        #skip first row as it contains header information
                row1=row
                cnt+=1
                continue    
            list1.append((row)) 

        list1.sort(key=lambda ro: (int(ro[0]),int(ro[1])))

    list1.insert(0, row1)
    with open(outputfilename,'wt') as csvfile1:
        writer=csv.writer(csvfile1, lineterminator='\n')
        for row in list1:
            writer.writerow(row)

但是我收到以下错误:

  File "C:\Users\50004182\Documents\temp.py", line 37, in <lambda>
    list1.sort(key=lambda ro: (int(ro[0]),int(ro[1])))
IndexError: list index out of range

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

您的文件中可能有一个空行。也许是最后一个。例如,您可以忽略空行:

def sortcsvfiles(inputfilename,outputfilename):
    with open(inputfilename,'rt') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)
        data = [row for row in reader if row] # ignore empty lines
        data.sort(key=lambda ro: (int(ro[0]),int(ro[1])))

    with open(outputfilename,'wt') as csvfile:
        writer=csv.writer(csvfile, lineterminator='\n')
        writer.writerow(header)
        writer.writerows(data)

答案 1 :(得分:2)

发生错误是因为您至少有一行没有2列。它可能有1或甚至0。

您可以在追加行之前对此进行测试:

if len(row) > 1:
    list1.append(row) 

要排除所有行但跳过第一个标题,您可以使用next()函数(请参阅https://github.com/turquoiseowl/i18n);使用sorted()函数:

def sortcsvfiles(inputfilename, outputfilename):
    with open(inputfilename,'rt') as csvfile1:
        reader = csv.reader(csvfile1)
        headers = next(reader, None)  # get one row, or None if there are no rows
        rows = sorted(
            (r for r in reader if len(r) > 1),
            key=lambda r: (int(r[0]), int(r[1])))

    with open(outputfilename,'wt') as csvfile1:
        writer = csv.writer(csvfile1, lineterminator='\n')
        if headers:
            writer.writerow(headers)
        writer.writerows(rows)

我使用writer.writerows()在一次调用中编写整个排序行列表。