两列CSV数据排序 - 一列str(升序)另一列日期(降序)

时间:2012-12-07 02:30:26

标签: python csv python-2.7 sorted

我很想知道如何执行两列的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
'''

1 个答案:

答案 0 :(得分: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))