2

我有兴趣知道如何执行两列排序的 CSV 文件,其中一列升序,另一列降序,需要解析为可理解的日期格式。

import operator
import csv
import dateutil.parser as dparser

reader = csv.reader(open("2002_NASDAQ.csv"), delimiter=",")

sortedlist = sorted(reader, key=lambda k: (k[0], dparser.parse(k[1])), reverse=True)

with open('2002_NASDAQ_out.csv', 'wb') as f:
    csv.writer(f).writerows(sortedlist)

如果我删除解析脚本运行没有错误。但是,如果没有正确格式的日期,结果将不符合预期(股票升序,日期降序)。

''' Sample sample.csv data
AAME,01-Jan-2002,2.204,2.204,2.204,2.204,0
AAON,01-Jan-2002,7.254,7.254,7.254,7.254,0
AAPL,01-Jan-2002,10.95,10.95,10.95,10.95,0
AAME,02-Jan-2002,5.71,5.71,5.71,5.71,0
AAON,02-Jan-2002,11.125,11.125,11.125,11.125,0
AAPL,02-Jan-2002,13.85,13.85,13.85,13.85,0
AAME,03-Jan-2002,28.82,28.82,28.82,28.82,0
AAON,03-Jan-2002,15.82,15.82,15.82,15.82,0
AAPL,03-Jan-2002,1.725,1.725,1.725,1.725,0
AAME,04-Jan-2002,5.3333,5.3333,5.3333,5.3333,0

''' Example sorted.csv data
AAME,04-Jan-2002,5.3333,5.3333,5.3333,5.3333,0
AAME,03-Jan-2002,28.82,28.82,28.82,28.82,0
AAME,02-Jan-2002,5.71,5.71,5.71,5.71,0
AAME,01-Jan-2002,2.204,2.204,2.204,2.204,0
AAON,03-Jan-2002,15.82,15.82,15.82,15.82,0
AAON,02-Jan-2002,11.125,11.125,11.125,11.125,0
 .
 .
 .
AAPL,03-Jan-2002,1.725,1.725,1.725,1.725,0
'''
4

1 回答 1

2

传统的做法是依靠 Python 的排序是稳定的,然后排序两次(注意先做第二个键):

a = sorted(something, key=itemgetter(1), reverse=True)
a.sort(key=itemgetter(0))

例子

>>> a = [ (1, 2), (0, 1), (2, 1), (2, 7) ]
>>> a.sort(key=itemgetter(1), reverse=True)
>>> a.sort(key=itemgetter(0))
>>> a
[(0, 1), (1, 2), (2, 7), (2, 1)]

未经测试

sortedlist = sorted(reader, key=lambda L: dparser.parse(L[1]), reverse=True)
sortedlist.sort(key=itemgetter(0))
于 2012-12-07T10:49:13.080 回答