Skip to content

Instantly share code, notes, and snippets.

@raviprakashgiri
Created February 3, 2016 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raviprakashgiri/26b587c47d5ce7ecb926 to your computer and use it in GitHub Desktop.
Save raviprakashgiri/26b587c47d5ce7ecb926 to your computer and use it in GitHub Desktop.
Sorting a comma separated file according to the columns
#there are three columns. This script is gonna sort the file first w.r.t. column 3 then further w.r.t. column 2
import csv
def getKey(item):
return int(item[0])
def getKey1(item):
return int(item[1])
def getKey2(item):
return int(item[2])
cr=csv.reader(open('combine_work.txt','rb'))
output_file1=open('sorted_combine_work.txt','w+')
all_elements=[]
for row in cr:
all_elements.append(row)
all_elements=sorted(all_elements,key=getKey2)
all_elements=sorted(all_elements,key=getKey1)
for i in range(0,len(all_elements)):
res=all_elements[i][0]+","+all_elements[i][1]+","+all_elements[i][2]+"\n"
output_file1.write(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment