2016-10-03 151 views
-1

我想从使用python的json数组中提取一个值。我如何在特定日期获得"energy"的价值?这里是JSON的样子:有条件地解析json

{ 
    "emeter": { 
    "get_daystat": { 
     "day_list": [ 
     { "year": 2016, "month": 10, "day": 1, "energy": 0.651000 }, 
     { "year": 2016, "month": 10, "day": 2, "energy": 0.349000 }, 
     { "year": 2016, "month": 10, "day": 3, "energy": 0.481000 } 
     ], 
     "err_code": 0 
    } 
    } 
} 

因此,例如,使用:

parsed_json = json.loads(json) 

我将如何提取"year":2016, "month":10, "day":2"energy"价值?

+0

无效JSON。我假设'json ='不在JSON文本字符串中。 –

+1

这里的*解析*没有任何条件。通过解析数据结构来提取它的一部分比试图有条件地解析(这是关于“活跃CS研究”领域的主题)更容易,更容易。 –

+0

顺便说一句,这是一个相当不幸的数据结构。如果这是“get_daystat”,则搜索单个值会更加高效:{“year”:{“2016”:{“10”:{“1”:0.651,“2”:0.349,“3 “:0.481}}}' –

回答

3

parsed_json将有一个python字典。您可以通过简单的线性搜索访问day_list阵列。

def get_energy_value_by_date(obj, year, month, day): 
    for value in obj['emeter']['get_daystat']['day_list']: 
     if value['year'] == year and value['month'] == month and value['day'] == day: 
      return value['energy'] 


energy = get_energy_value_by_date(parsed_json, 2016, 10, 2) 
+0

谢谢。那完美的作品。 – Baobab

0

你可以通过数据做线性搜索:

def get_energy(data, year, month, day): 
    for date in data['emeter']['get_daystat']['day_list']: 
     if(date['year'] == year 
      and date['month'] == month 
      and date['day'] == day): 
      return date['energy'] 

json_data = { 
    "emeter": { 
    "get_daystat": { 
     "day_list": [ 
     { "year": 2016, "month": 10, "day": 1, "energy": 0.651000 }, 
     { "year": 2016, "month": 10, "day": 2, "energy": 0.349000 }, 
     { "year": 2016, "month": 10, "day": 3, "energy": 0.481000 } 
     ], 
     "err_code": 0 
    } 
    } 
} 

print('{:1.6f}'.format(get_energy(json_data, 2016, 10, 2))) # --> 0.349000 

如果没有匹配的日期,该函数将返回有效None

*更新*

如果按日期在"day_list"他们排序(排序)了很多天如在你的榜样,它会更快地利用这个优势,做二进制搜索而不是线性搜索。 Python包含bisect模块,可用于执行简单的二进制搜索。不幸的是,其中的任何函数都没有使用可选参数来控制比较,如sorted()函数所做的那样。

然而,这可以通过查看source code的模块和编写自己的搜索功能来克服,如下图所示:

from datetime import datetime 

def keyed_bisect_left(a, x, lo=0, hi=None, keyfunc=lambda v: v): 
    """Return the index where to insert item x in list a, assuming a is sorted. 

    Like bisect.bisect_left but allows a keyfunc to extract key from each item. 
    """ 
    x_key = keyfunc(x) 
    if lo < 0: 
     raise ValueError('lo must be non-negative') 
    if hi is None: 
     hi = len(a) 
    while lo < hi: 
     mid = (lo+hi) // 2 
     if keyfunc(a[mid]) < x_key: 
      lo = mid+1 
     else: 
      hi = mid 
    return lo 

def get_date(d): 
    return datetime(d['year'], d['month'], d['day']) 

def get_energy2(data, year, month, day): 
    """Locate the first day exactly equal to the given date.""" 
    day_list = data['emeter']['get_daystat']['day_list'] 
    target = {'year': year, 'month': month, 'day': day} 

    i = keyed_bisect_left(day_list, target, keyfunc=get_date) 

    # return energy value if date found 
    if i != len(day_list) and get_date(day_list[i]) == get_date(target): 
     return day_list[i]['energy'] 

    raise ValueError('date not found') 

print('{:1.6f}'.format(get_energy2(json_data, 2016, 10, 1))) 
print('{:1.6f}'.format(get_energy2(json_data, 2016, 10, 2))) 
print('{:1.6f}'.format(get_energy2(json_data, 2016, 10, 3))) 
print('{:1.6f}'.format(get_energy2(json_data, 2016, 10, 4))) 

输出:

0.651000 
0.349000 
0.481000 
Traceback (most recent call last): 
    File "conditionally-parsing-json.py", line 67, in <module> 
    print('{:1.6f}'.format(get_energy2(json_data, 2016, 10, 4))) # --> ValueError 
    File "conditionally-parsing-json.py", line 62, in get_energy2 
    raise ValueError('date not found') 
ValueError: date not found