我有兴趣知道如何执行两列排序的 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
'''