What is the best way to sort a csv file in python with multiple columns on date?
-
I have a csv file with records in the following format, first I need to sort by Invoice and then By DateTo, I just wanted to see if the sort function works on DateTo and noticed that '1/5/2013' shows up after 1/26/2013, but changing it to '1/05/2013' makes it right. The same issue arises on Month. Str Num Date TO DateFrom 10011, 1/12/2013 0:00:00, 1/6/2013 0:00:00 10011, 1/19/2013 0:00:00, 1/13/2013 0:00:00 10011, 1/26/2013 0:00:00, 1/20/2013 0:00:00 10011, 1/5/2013 0:00:00, 12/30/2012 0:00:00 10011, 10/13/2012 0:00:00,10/7/2012 0:00:00
-
Answer:
There are two native Python libraries that will help you out here: csv and datetime. [code python] import csv with open('sample.csv') as f: for line in csv.reader(f): # Each line is a list of values, do what you need to with it import datetime datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") # creates a "datetime" object [/code] Combine these with Python's built in "sorted" function, and you should be good to go. http://docs.python.org/2/library/functions.html#sorted http://docs.python.org/2/library/datetime.html http://docs.python.org/2/library/csv.html
Harold Kingsberg at Quora Visit the source
Other answers
Import your file using the pandas library, which will likely be less effort than using pure python.
Randy Zwitch
"Note that sorted() is applied to each line and not to the column entries. You would need to import the operator module and use operator.itemgetter(). Since the entries in a CSV file are strings, the sorting will place a nmber like "90" as 'higher' than a nuber like "864". This should not affect your dates, but would cause a problem with values. This would have to be handled seperately. I have not yet examined sorted() to determine if this can be avoided without conversion. import csv import operator ifile =open('myfile.csv', 'rb') infile = csv.reader(ifile) # The first entry is the header line infields = infile.next() statindex = infields.index('Desired Header') # create the sorted list sortedlist = sorted(infile, key=operator.itemgetter(statindex), reverse=True) ifile.close # open the output file - it can be the same as the input file ofile = open('myoutput.csv, 'wb') # write the header outfile.writerow(infields) # write the sorted list for row in sortedlist: outfile.writerow(row) # processing finished, close the output file ofile.close() UPDATE: If any of the rows in the entire file contain non-numeric data, then the sorted MUST sort by a text method. This includes if the header line is not row 1 (for example follows a title line). or if there is a blank entry in the column. If all entries in that column are numeric then it can be sorted as such. For example: try: sortedlist = sorted(infile, key=lambda d: float(d[statistic]), reverse=True) except ValueError: sortedlist = sorted(infile, key=lambda d: d[statistic], reverse=True)
Hillel Markowitz
Related Q & A:
- What is the best way to calculate a date difference?Best solution by Stack Overflow
- What is the best way to count lines in file?Best solution by Stack Overflow
- What's the best way to get a job in a restaurant?Best solution by Yahoo! Answers
- What's the best way to make a good impression at a job interview?Best solution by Yahoo! Answers
- What is the best way to negotiate a salary for a new position?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.