sort by column a csv file case insensitive

2012-04-16 Thread Lee Chaplin
Well, if I have a file like this one:

EWIENER,
edit,
edgard,
evan,
erick,
elliott,
enquiries,
Elliott,

I would like to get something like this (sorted by column 4 and case
insensitive):

edgard,
edit,
elliott,
Elliott,
enquiries,
erick,
evan,
EWIENER,

(Obviously, there are more data in the other columns, I edited the
file for clarity.)

>From the previous email I would like to call:
sortcsvbyfield('e.txt', 4)

I am on python 2.6 on Win.

Thanks,
Lee
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sort by column a csv file case insensitive

2012-04-15 Thread Peter Otten
Lee Chaplin wrote:

> 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.


Try out your sort key on interactively:

>>> import csv
>>> import operator
>>> columnnumber = 0
>>> sortkey = lambda x: (operator.itemgetter(columnnumber), x[0].lower())
>>> sortkey(["an", "example", "row"])
(, 'an')
>>> sortkey(["an", "example", "row"])
(, 'an')
>>> sortkey(["an", "example", "row"])
(, 'an')

Effectively you are sorting your data by the id (memory address) of the 
itemgetters you create. You probably want

>>> def sortkey(row):
... column = row[columnnumber]
... return column.lower(), column
... 
>>> sorted([[col] for col in "alpha ALPHA beta GAMMA gamma".split()], 
key=sortkey)
[['ALPHA'], ['alpha'], ['beta'], ['GAMMA'], ['gamma']]

Alternatively you can use

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'de_DE.UTF-8'
>>> sorted([[col] for col in "alpha ALPHA beta GAMMA gamma".split()], 
key=lambda row: locale.strxfrm(row[columnnumber]))
[['alpha'], ['ALPHA'], ['beta'], ['gamma'], ['GAMMA']]


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sort by column a csv file case insensitive

2012-04-15 Thread Dave Angel
On 04/16/2012 01:11 AM, Lee Chaplin wrote:
> 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:

Could you perhaps qualify that "no success" bit?  Do you mean it didn't
output a file, or that the file wasn't sorted right, or that the data
wasn't in the file at all, or that you got an exception, or that you got
a syntax error, or that your disk filled up, or what?  Please paste the
results of running it, along with a brief description of why that was
not a success.

It might also be useful to state your working environment, like you're
running 32 bit CPython version 1.7 on Windows 2009, or whatever.  in
this case, I don't think it matters, but somebody else may know differently.

> 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


-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


sort by column a csv file case insensitive

2012-04-15 Thread Lee Chaplin
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
-- 
http://mail.python.org/mailman/listinfo/python-list