文本中的数据格式,
2010-04-16,9:15:00,3450,3488,3450,3470
解析文本,
Utuple = collections.namedtuple('Utuple', 'DT,OpenPrice,ClosePrice,HighPrice,LowPrice')
stats = collections.Counter()
for line in data.readlines():
cols = line.split(',')
Date = cols[0]
d = Date.split('-')
Time = cols[1]
t = Time.split(':')
DT = datetime(int(d[0]), int(d[1]), int(d[2]), int(t[0]), int(t[1]), int(t[2]))
DT = mdates.date2num(DT)
OpenPrice = float(cols[2])
HighPrice = float(cols[3])
LowPrice = float(cols[4])
ClosePrice = float(cols[5])
stats[DT] = Utuple(DT,OpenPrice,ClosePrice,HighPrice,LowPrice)
我想获得一个元组列表以适应candlesticks
matplotlib.finance 中的格式,预计为
D = [(datetime.datetime(2010, 4, 16, 9, 30), 311, 332, 344, 311),
(datetime.datetime(2010, 4, 16, 9, 31), 312, 332, 344, 311),
(datetime.datetime(2010, 4, 16, 9, 32), 323, 332, 344, 320),
(datetime.datetime(2010, 4, 16, 13, 0), 331, 332, 344, 330),
(datetime.datetime(2010, 4, 16, 13, 1), 335, 342, 348, 333)]
我做到了:
formated_data = []
for time, index in stats.items():
formated_data.append(tuple(index))
我想保持这个顺序。但是formated_data
,事实证明,13
datetime.datetime 中第四列中带有 的行最终位于带有 的行的前面9
。如何保持元组的顺序by the order that I save them
或按顺序the value of the number (9 < 13)
?