如何使用特定列使用python对csv文件中的数据进行排序

时间:2019-05-02 06:26:31

标签: python csv

我正在从csv文件中读取数据,并尝试通过使用特定列对数据进行排序,例如从csv文件中读取数据给100名学生,并且必须根据标记对数据进行排序

import csv
import operator

with open('Student_Records.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        print(row)
sortedlist = sorted(reader, key=operator.itemgetter(7), reverse=True)

for eachline in sortedlist:
    print(eachline)

csvFile.close()

excel工作表中的csv文件,该文件没有列名,以下是csv文件数据

1,Lois,Walker,F,lois.walker@hotmail.com,Donald Walker,Helen Walker,40,303-572-8492
2,Brenda,Robinson,F,brenda.robinson@gmail.com,Raymond Robinson,Judy Robinson,80,225-945-4954
3,Joe,Robinson,M,joe.robinson@gmail.com,Scott Robinson,Stephanie Robinson,70,219-904-2161
4,Diane,Evans,F,diane.evans@yahoo.com,Jason Evans,Michelle Evans,90,215-793-6791
5,Benjamin,Russell,M,benjamin.russell@charter.net,Gregory Russell,Elizabeth Russell,56,262-404-2252
6,Patrick,Bailey,M,patrick.bailey@aol.com,Ralph Bailey,Laura Bailey,36,319-812-6957
7,Nancy,Baker,F,nancy.baker@bp.com,Scott Baker,Judy Baker,78,229-336-5117

3 个答案:

答案 0 :(得分:1)

下面应该为您工作,我在读取csv之后创建了一个行列表,以使标记实际上是整数,而不是从csv读取时是字符串

我还假设csv中有多个空格,因此我使用了一个空格分隔符,因此itemgetter索引被选择为9,这可能因您的csv外观而异

import csv
import operator

li = []

#Open csv file
with open('file.csv', 'r') as csvFile:
    reader = csv.reader(csvFile, delimiter=' ', skipinitialspace=True )

    #Create a list of all rows such that the marks column is an integer
    for item in reader:
        #Save marks value as an integer, leave other values as is
        l = [int(value) if idx == 9 else value for idx, value in enumerate(item)]
        li.append(l)

#Sort on that item
print(sorted(li, key=operator.itemgetter(9), reverse=True))

我的csv如下:

1   Lois    Walker  F   lois.walker@hotmail.com Donald Walker   Helen Walker    40  303-572-8492
2   Brenda  Robinson    F   brenda.robinson@gmail.com   Raymond Robinson    Judy Robinson   80  225-945-4954
3   Joe Robinson    M   joe.robinson@gmail.com  Scott Robinson  Stephanie Robinson  70  219-904-2161
4   Diane   Evans   F   diane.evans@yahoo.com   Jason Evans Michelle Evans  90  215-793-6791
5   Benjamin    Russell M   benjamin.russell@charter.net    Gregory Russell Elizabeth Russell   56  262-404-2252

输出看起来像

[['4', 'Diane', 'Evans', 'F', 'diane.evans@yahoo.com', 'Jason', 'Evans', 'Michelle', 'Evans', 90, '215-793-6791'], 
['2', 'Brenda', 'Robinson', 'F', 'brenda.robinson@gmail.com', 'Raymond', 'Robinson', 'Judy', 'Robinson', 80, '225-945-4954'], 
['3', 'Joe', 'Robinson', 'M', 'joe.robinson@gmail.com', 'Scott', 'Robinson', 'Stephanie', 'Robinson', 70, '219-904-2161'], 
['5', 'Benjamin', 'Russell', 'M', 'benjamin.russell@charter.net', 'Gregory', 'Russell', 'Elizabeth', 'Russell', 56, '262-404-2252'], 
['1', 'Lois', 'Walker', 'F', 'lois.walker@hotmail.com', 'Donald', 'Walker', 'Helen', 'Walker', 40, '303-572-8492']]

答案 1 :(得分:1)

尝试熊猫,

df = pd.read_csv("your_file", sep='xx', 
              names = ["x", "y", "z", "marks"])

df.sort_values('marks')

print(df)

答案 2 :(得分:1)

您可以尝试

import csv
with open('input.csv', newline='') as csvfile:
    rdr = csv.reader(csvfile)
    l = sorted(rdr, key=lambda x: x[6], reverse=True)

csv.reader()用于创建读取器对象,该对象使用sorted()reverse=True进行降序排序以获得列表。

该列表可用于使用类似

的格式写出输出csv
with open('output.csv', 'w') as csvout:
    wrtr = csv.writer(csvout)
    wrtr.writerows(l)

输出的csv文件类似于

4,Diane   Evans,F,diane.evans@yahoo.com,Jason Evans,Michelle Evans,90,215-793-6791
2,Brenda  Robinson,F,brenda.robinson@gmail.com,Raymond Robinson,Judy Robinson,80,225-945-4954
3,Joe Robinson,M,joe.robinson@gmail.com,Scott Robinson,Stephanie Robinson,70,219-904-2161
5,Benjamin    Russell,M,benjamin.russell@charter.net,Gregory Russell,Elizabeth Russell,56,262-404-2252
1,Lois  Walker,F,lois.walker@hotmail.com,Donald Walker,Helen Walker,40,303-572-8492

由于您正在从文件对象读取数据,因此将newline参数指定为''是安全的。

如文档所述:

  

如果csvfile是文件对象,则应使用newline =''将其打开。

来自docs

  

如果未指定newline ='',嵌入引号中的换行符将无法正确解释,并且在使用\ r \ n linendings的平台上将添加一个额外的\ r。指定newline =''应该总是安全的,因为csv模块会执行自己的(通用)换行处理。