Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting CSV in Python

I assumed sorting a CSV file on multiple text/numeric fields using Python would be a problem that was already solved. But I can't find any example code anywhere, except for specific code focusing on sorting date fields.

How would one go about sorting a relatively large CSV file (tens of thousand lines) on multiple fields, in order?

Python code samples would be appreciated.

like image 370
Pranab Avatar asked Jan 18 '10 20:01

Pranab


1 Answers

Python's sort works in-memory only; however, tens of thousands of lines should fit in memory easily on a modern machine. So:

import csv

def sortcsvbymanyfields(csvfilename, themanyfieldscolumnnumbers):
  with open(csvfilename, 'rb') as f:
    readit = csv.reader(f)
    thedata = list(readit)
  thedata.sort(key=operator.itemgetter(*themanyfieldscolumnnumbers))
  with open(csvfilename, 'wb') as f:
    writeit = csv.writer(f)
    writeit.writerows(thedata)
like image 122
Alex Martelli Avatar answered Oct 06 '22 08:10

Alex Martelli