我正在制作加密货币分析脚本,交换API将市场价差捆绑在一个JSON对象中,打印出来 -
{'error': [], 'result': {'XXBTZEUR': {'asks': [['892.00000', '0.067',
1489866640]], 'bids': [['891.86000', '0.032', 1489866639]]}}}
如您所见,这是一个多维数组。让我简化一下它的结构。
c = {'a':[],'b':{'bsubarray':{'asks':[[x1,y1,z1],[x2,y2,z2],...]
{'bids':[[x1,y1,z1],[x2,y2,z2],...]}
我试图从此ask_x
和ask_y
以及bid_x
和bid_y
这是我在python中的第三个程序,所以任何详细的解释都会对我有所帮助。
谢谢!
答案 0 :(得分:0)
要提取ask_x
,ask_y
等,您只需要zip
提升相关的asks
等,例如:
>>> c = {'a':[],'b':{'bsubarray':{'asks':[['x1','y1','z1'],['x2','y2','z2'],['x3','y3','z3']],
... 'bids':[['x1','y1','z1'],['x2','y2','z2'],['x3','y3','z3']]}}}
>>> ask_x, ask_y, ask_z = zip(*c['b']['bsubarray']['asks'])
>>> bid_x, bid_y, bid_z = zip(*c['b']['bsubarray']['bids'])
>>> ask_x
('x1', 'x2', 'x3')
>>> ask_y
('y1', 'y2', 'y3')
使用您的实际数据:
>>> c = {'error': [], 'result': {'XXBTZEUR': {'asks': [['892.00000', '0.067', 1489866640]],
... 'bids': [['891.86000', '0.032', 1489866639]]}}}
>>> ask_x, ask_y, ask_z = zip(*c['result']['XXBTZEUR']['asks'])
>>> bid_x, bid_y, bid_z = zip(*c['result']['XXBTZEUR']['bids'])
>>> ask_x
('892.00000',)
>>> ask_y
('0.067',)