Sorting a CSV file

Given a files salaries.csv with this structure:
City,Job,Salary
Delhi,Doctors,500
Delhi,Lawyers,400
Delhi,Plumbers,100
London,Doctors,800
London,Lawyers,700
London,Plumbers,300
Tokyo,Doctors,900
Tokyo,Lawyers,800
Tokyo,Plumbers,400
...
... sort cities by descending order of lawyer salary.

Sample output

Tokyo,800
London,700
Delhi,400
... 
 
 
 

solution

 

import csv
fin=open('salaries.csv','rb')
reader = csv.reader(fin)
reader.next()
sorted_salary=sorted(reader, key=lambda t: int( t[2] ), reverse=True)
for row in sorted_salary:
    if row[1] == 'Lawyers':
       print (row[0],row[1],row[2])

No comments:

Post a Comment