API下载数据,建议?

时间:2018-07-09 21:29:04

标签: python database string pandas extract

我正在尝试从API解码数据,只是想不出一种干净的方法来提取值和时间值。我一直在尝试进行字符串操作,但最终变得非常复杂。

{"max_scale": "0", "min_scale": "0", "graph_label": "Light Level", "average": "1", "length_of_time": "3600", "upper_warn": "1000", "lower_warn": "30", "cached": false, "values": 
 [{"value": 0.0, "time": 1531170219}, 
  {"value": 0.0, "time": 1531170159}, 
  {"value": 0.0, "time": 1531170099}, 
  {"value": 0.0, "time": 1531170039}, 
  {"value": 0.0, "time": 1531169979}, 
  {"value": 0.0, "time": 1531169919}, 
  {"value": 0.0, "time": 1531169859}, 
  {"value": 0.0, "time": 1531169799}, 
  {"value": 0.0, "time": 1531169739}, 
  {"value": 0.0, "time": 1531169679}, 
  {"value": 0.0, "time": 1531169619}, 
  {"value": 0.0, "time": 1531166679}], 
  "timestamp_to": "1531170222.798", "format_string": "%f Lux"}

2 个答案:

答案 0 :(得分:0)

这是JSON格式。使用python json编码器/解码器加载此数据。它将变成字典,类似

my_json_dict['values'] 

将返回该列表。

答案 1 :(得分:0)

您可以为此使用Python的json模块,或者您可以自己修改数据并将其转换为dict,因为实际上它已经是一个了。然后,您需要做的就是像这样遍历它:

    data = {"max_scale": "0", "min_scale": "0", "graph_label": 
            "Light Level", "average": "1", "length_of_time": "3600",
            "upper_warn": "1000", "lower_warn": "30", "cached": 'false', 
            "values": 
              [{"value": 0.0, "time": 1531170219}, 
               {"value": 0.0, "time": 1531170159}, 
               {"value": 0.0, "time": 1531170099}, 
               {"value": 0.0, "time": 1531170039}, 
               {"value": 0.0, "time": 1531169979}, 
               {"value": 0.0, "time": 1531169919}, 
               {"value": 0.0, "time": 1531169859}, 
               {"value": 0.0, "time": 1531169799}, 
               {"value": 0.0, "time": 1531169739}, 
               {"value": 0.0, "time": 1531169679}, 
               {"value": 0.0, "time": 1531169619}, 
               {"value": 0.0, "time": 1531166679}], 
               "timestamp_to": "1531170222.798", "format_string": "%f Lux"}

for item in data['values']:
    time, value = item['time'], item['value']