sort by column a csv file case insensitive

Lee Chaplin lchaplin13 at gmail.com
Mon Apr 16 01:11:09 EDT 2012


Hi all,

I am trying to sort, in place, by column, a csv file AND sort it case
insensitive.
I was trying something like this, with no success:

import csv
import operator

def sortcsvbyfield(csvfilename, columnnumber):
  with open(csvfilename, 'rb') as f:
    readit = csv.reader(f)
    thedata = list(readit)

  thedata = sorted(thedata, key = lambda x:
(operator.itemgetter(columnnumber) ,x[0].lower()))  #!!!
  with open(csvfilename, 'wb') as f:
    writeit = csv.writer(f)
    writeit.writerows(thedata)

The line marked is the culprit.
Any help is greatly appreciated.

Thanks,
Lee



More information about the Python-list mailing list