简体   繁体   English

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

[英]List index out of range while sorting in python

I am trying to read a CSV file into a list and then sort it based on the first two columns of the list (first by first column and then by second column if the first column is the same). 我正在尝试将CS​​V文件读取到列表中,然后根据列表的前两列对其进行排序(如果第一列相同,则按第一列,然后按第二列)。 This is what I am doing: 这就是我在做什么:

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)

But I am getting the following error: 但是我收到以下错误:

  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

How can I fix this? 我怎样才能解决这个问题?

You have probably an empty line in your file. 您的文件中可能有一个空行。 Perhaps the last one. 也许是最后一个。 For example, you can just ignore empty lines: 例如,您可以忽略空行:

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)

The error occurs because you have at least one row that does not have 2 columns. 发生错误是因为您至少有一行没有2列。 It may have 1 or even 0 instead. 它可能有1甚至0。

You could test for this before appending the row: 您可以在添加行之前对此进行测试:

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

To sort all rows but skip the first header, you can use the next() function (see a previous answer of mine ); 要对所有行进行排序,但跳过第一个标头,可以使用next()函数(请参见mine的先前答案 ); using the sorted() function perhaps: 使用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)

I used writer.writerows() to write the whole list of sorted rows in one call. 我使用writer.writerows()在一次调用中写入了排序行的整个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM