1

I'm reading data from csv file and trying to sort data by using particular column for example reading data to 100 students from csv file and have to sort data according to marks

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()

csv file in excel sheet and that file doesn't have column names, following is the csv file data

1,Lois,Walker,F,[email protected],Donald Walker,Helen Walker,40,303-572-8492
2,Brenda,Robinson,F,[email protected],Raymond Robinson,Judy Robinson,80,225-945-4954
3,Joe,Robinson,M,[email protected],Scott Robinson,Stephanie Robinson,70,219-904-2161
4,Diane,Evans,F,[email protected],Jason Evans,Michelle Evans,90,215-793-6791
5,Benjamin,Russell,M,[email protected],Gregory Russell,Elizabeth Russell,56,262-404-2252
6,Patrick,Bailey,M,[email protected],Ralph Bailey,Laura Bailey,36,319-812-6957
7,Nancy,Baker,F,[email protected],Scott Baker,Judy Baker,78,229-336-5117
12
  • 1
    I think you have done it yourself: key=operator.itemgetter(7), you need to change your key. Post a part of your csv for further help? Commented May 2, 2019 at 6:30
  • 1
    Edit your question with this. Commented May 2, 2019 at 6:33
  • 1
    You might want to edit your question and ad a formatted part of your csv to your question Commented May 2, 2019 at 6:34
  • 1
    @GeethaTabjul Is the input a csv or tsv? There are no commas. Commented May 2, 2019 at 6:41
  • 1
    Where are the commas then? what is the delimiter being used? Commented May 2, 2019 at 6:47

3 Answers 3

2

You could try

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() is used to create a reader object which is sorted using sorted() with reverse=True for descending order sort to obtain a list.

This list can be used to write out an output csv using something like

with open('output.csv', 'w') as csvout:
    wrtr = csv.writer(csvout)
    wrtr.writerows(l)

The output csv file would be something like

4,Diane   Evans,F,[email protected],Jason Evans,Michelle Evans,90,215-793-6791
2,Brenda  Robinson,F,[email protected],Raymond Robinson,Judy Robinson,80,225-945-4954
3,Joe Robinson,M,[email protected],Scott Robinson,Stephanie Robinson,70,219-904-2161
5,Benjamin    Russell,M,[email protected],Gregory Russell,Elizabeth Russell,56,262-404-2252
1,Lois  Walker,F,[email protected],Donald Walker,Helen Walker,40,303-572-8492

Since you are reading the data from a file object, specify the newline parameter as '' to be safe.

As the docs say:

If csvfile is a file object, it should be opened with newline=''.

From docs:

If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

Sign up to request clarification or add additional context in comments.

Comments

1

Below should work for you, I created a list of rows after reading the csv such that the marks are actually integers, instead of strings when they are read from the csv

Also I am assuming multiple whitespaces in csv, so I have used a whitespace delimiter so itemgetter index is chosen as 9, which might be different based on how your csv looks like

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))

My csv looks like:

1   Lois    Walker  F   [email protected] Donald Walker   Helen Walker    40  303-572-8492
2   Brenda  Robinson    F   [email protected]   Raymond Robinson    Judy Robinson   80  225-945-4954
3   Joe Robinson    M   [email protected]  Scott Robinson  Stephanie Robinson  70  219-904-2161
4   Diane   Evans   F   [email protected]   Jason Evans Michelle Evans  90  215-793-6791
5   Benjamin    Russell M   [email protected]    Gregory Russell Elizabeth Russell   56  262-404-2252

The output will look like

[['4', 'Diane', 'Evans', 'F', '[email protected]', 'Jason', 'Evans', 'Michelle', 'Evans', 90, '215-793-6791'], 
['2', 'Brenda', 'Robinson', 'F', '[email protected]', 'Raymond', 'Robinson', 'Judy', 'Robinson', 80, '225-945-4954'], 
['3', 'Joe', 'Robinson', 'M', '[email protected]', 'Scott', 'Robinson', 'Stephanie', 'Robinson', 70, '219-904-2161'], 
['5', 'Benjamin', 'Russell', 'M', '[email protected]', 'Gregory', 'Russell', 'Elizabeth', 'Russell', 56, '262-404-2252'], 
['1', 'Lois', 'Walker', 'F', '[email protected]', 'Donald', 'Walker', 'Helen', 'Walker', 40, '303-572-8492']]

Comments

1

Try Pandas,

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

df.sort_values('marks')

print(df)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.