Created
February 3, 2016 14:40
-
-
Save raviprakashgiri/26b587c47d5ce7ecb926 to your computer and use it in GitHub Desktop.
Sorting a comma separated file according to the columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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