如何从Python中的此JSON(日期时间+价格)源中提取数据?

时间:2017-07-27 14:12:20

标签: python json

我通过交易平台获得此JSON数据:

{u'prices':                          bid                                 ask           \
                        Open     High      Low    Close     Open     High   
DateTime                                                                    
2016:01:01-05:00:00  10373.0  10580.0   9235.0   9703.0  10373.0  10601.0   
2016:02:01-05:00:00   9673.0   9906.0   9196.0   9675.0   9713.0   9929.0   
2016:03:01-05:00:00   9726.0  11036.0   9646.0  10899.0   9784.0  11045.0   
2016:04:01-05:00:00  10838.0  11234.0   9247.0   9371.0  10844.0  11243.0   
2016:05:01-05:00:00   9374.0  10068.0   8936.0   9983.0   9400.0  10077.0   
2016:06:01-05:00:00   9991.0   9994.0   9146.0   9536.0   9999.0  10044.0   
2016:07:01-05:00:00   9526.0  10462.0   9433.0  10418.0   9544.0  10472.0   
2016:08:01-05:00:00  10428.0  11018.0  10334.0  10595.0  10444.0  11027.0   
2016:09:01-05:00:00  10610.0  11612.0  10171.0  11293.0  10603.0  11622.0   
2016:10:01-05:00:00  11297.0  12180.0  11197.0  11350.0  11305.0  12192.0   
2016:11:01-04:00:00  11374.0  11374.0  10404.0  11055.0  11358.0  11387.0   
2016:12:01-05:00:00  11061.0  11795.0  10825.0  11568.0  11069.0  11804.0   
2017:01:01-05:00:00  11572.0  12555.0  11474.0  12498.0  11623.0  12565.0   
2017:02:01-05:00:00  12447.0  13743.0  12447.0  13698.0  12506.0  13752.0   
2017:03:01-05:00:00  13701.0  14445.0  13701.0  14366.0  13709.0  14454.0   
2017:04:01-05:00:00  14306.0  14541.0  14002.0  14361.0  14434.0  14550.0   
2017:05:01-05:00:00  14366.0  15696.0  14341.0  15278.0  14374.0  15739.0   
2017:06:01-05:00:00  15236.0  15592.0  14161.0  14404.0  15286.0  15604.0   
2017:07:01-05:00:00  14401.0  15432.0  13309.0  15422.0  14480.0  15443.0   

                                      spread                     last        \
                         Low    Close   Open  High    Low Close  Open  High   
DateTime                                                                      
2016:01:01-05:00:00   9244.0   9714.0    0.0  21.0    9.0  11.0  None  None   
2016:02:01-05:00:00   9215.0   9691.0   40.0  23.0   19.0  16.0  None  None   
2016:03:01-05:00:00   9737.0  10911.0   58.0   9.0   91.0  12.0  None  None   
2016:04:01-05:00:00   9256.0   9382.0    6.0   9.0    9.0  11.0  None  None   
2016:05:01-05:00:00   8949.0  10000.0   26.0   9.0   13.0  17.0  None  None   
2016:06:01-05:00:00   9155.0   9559.0    8.0  50.0    9.0  23.0  None  None   
2016:07:01-05:00:00   9442.0  10431.0   18.0  10.0    9.0  13.0  None  None   
2016:08:01-05:00:00  10372.0  10609.0   16.0   9.0   38.0  14.0  None  None   
2016:09:01-05:00:00  10186.0  11304.0   -7.0  10.0   15.0  11.0  None  None   
2016:10:01-05:00:00  11234.0  11359.0    8.0  12.0   37.0   9.0  None  None   
2016:11:01-04:00:00  10413.0  11070.0  -16.0  13.0    9.0  15.0  None  None   
2016:12:01-05:00:00  10834.0  11587.0    8.0   9.0    9.0  19.0  None  None   
2017:01:01-05:00:00  11483.0  12512.0   51.0  10.0    9.0  14.0  None  None   
2017:02:01-05:00:00  12472.0  13713.0   59.0   9.0   25.0  15.0  None  None   
2017:03:01-05:00:00  13709.0  14379.0    8.0   9.0    8.0  13.0  None  None   
2017:04:01-05:00:00  14011.0  14374.0  128.0   9.0    9.0  13.0  None  None   
2017:05:01-05:00:00  14374.0  15289.0    8.0  43.0   33.0  11.0  None  None   
2017:06:01-05:00:00  14194.0  14414.0   50.0  12.0   33.0  10.0  None  None   
2017:07:01-05:00:00  14250.0  15438.0   79.0  11.0  941.0  16.0  None  None   


                      Low Close     Volume  
DateTime                                    
2016:01:01-05:00:00  None  None  708512208  
2016:02:01-05:00:00  None  None  496710765  
2016:03:01-05:00:00  None  None  445406899  
2016:04:01-05:00:00  None  None  468757810  
2016:05:01-05:00:00  None  None  469025253  
2016:06:01-05:00:00  None  None  437172919  
2016:07:01-05:00:00  None  None  448678799  
2016:08:01-05:00:00  None  None  359985844  
2016:09:01-05:00:00  None  None  594549768  
2016:10:01-05:00:00  None  None  409916710  
2016:11:01-04:00:00  None  None  431415176  
2016:12:01-05:00:00  None  None  361497431  
2017:01:01-05:00:00  None  None  332480596  
2017:02:01-05:00:00  None  None  315665923  
2017:03:01-05:00:00  None  None  314834247  
2017:04:01-05:00:00  None  None  212274515  
2017:05:01-05:00:00  None  None  339033935  
2017:06:01-05:00:00  None  None  367915336  
2017:07:01-05:00:00  None  None  193057585  ,

u'instrumentType':  u'SHARES',
u'allowance':     { u'remainingAllowance': 9531,
                    u'allowanceExpiry':   32328,
                    u'totalAllowance':    10000
                    }
}

我无法弄清楚如何拉日期时间?我可以通过以下方式提取开放率:

open = parsed_json_data["prices"]['bid']['Open']

但是当我尝试为DateTime做同样的事情时:

datetime = parsed_json_data["prices"]['bid']['DateTime']

我得到KeyError。我怎么去拉日期时间?

1 个答案:

答案 0 :(得分:5)

parsed_json_data["prices"]似乎是pandas DataFrameDateTime是其索引。您可以parsed_json_data["prices"].index访问它。