I am trying to sort excel data using 2 or 3 columns (so a nested sort if you will). Currently what occurs is I get a text file (some times more than 1) with log entries, I then have to convert the entries into a managed Excel spread sheet.
I have managed to do this, but now my manager wants me to also sort it by the 'date' and 'time' columns respectively.
So to break it down I am just trying to figure out how I can do this.
Requirements:
Sort a csv or excel spreadsheet via multiple columns and write them to a document (of the same type preferably).
CODE:
Currently the code I have is:
import csv, os
import operator
testcsv= open('test.csv', 'r')
csvSort = csv.reader(testcsv, delimiter=",")
sort = sorted(csvSort,key=operator.itemgetter(0,1))
for eachline in sort:
print eachline
This works fine for "printing" and checking. But I can't get this to write the data back into an excel spreadsheet.
If I did that it goes all into 1 row instead of multiple rows. When I do the write lines to files this is the code I use:
dest = "C:/Test2.csv"
for eachline in sort:
if not os.path.exists(dest):
fileToCreate = open(dest, 'w')
fileToCreate.writelines(eachline)
else:
fileToCreate = open(dest, 'a+')
fileToCreate.writelines(eachline)
fileToCreate.close()
Perhaps if someone has a better option for me to use or something to add that would help me, I'd be grateful.
Test input data file which is being used
05/02/2015,20:22:24,201534
07/02/2015,20:23:24,201534
05/02/2015,20:23:24,201534
06/02/2015,20:23:24,201534
05/02/2015,20:24:24,201534
13/02/2015,20:24:24,201534
13/02/2015,20:24:24,201534
12/02/2015,20:21:24,201534
01/02/2015,20:21:24,201534
21/02/2015,20:21:24,201534
21/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
13/02/2015,20:21:24,201534
05/02/2015,20:21:24,201534
05/02/2015,20:21:24,201534
05/02/2015,20:21:24,201534
05/02/2015,20:18:24,201534
05/02/2015,20:18:24,201534
05/02/2015,20:18:24,201534
05/02/2015,20:22:24,201534