将InfluxDB与influxdb-python
结合使用时,我得到以下结果:
results = {'series': [{'columns': ['time', 'v'],
'name': 'wind-angle',
'values': [['2018-12-12T10:25:57.890567522Z', 2.9],
['2018-12-12T10:25:57.918715188Z', 285],
['2018-12-12T10:25:57.935111522Z', 3.7],
['2018-12-12T10:25:57.957419855Z', 288.7],
['2018-12-12T10:25:57.973892188Z', 6.3]]},
{'columns': ['time', 'v'],
'name': 'wind-spd',
'values': [['2018-12-12T10:25:57.888505188Z', 1.44],
['2018-12-12T10:25:57.916779855Z', 1.44],
['2018-12-12T10:25:57.932662855Z', 1.44],
['2018-12-12T10:25:57.955199188Z', 1.49],
['2018-12-12T10:25:57.971157855Z', 1.54]]}],
'statement_id': 0}
我的目标是在相同的测量时间戳下合并两个值,如下所示:
{
't': 2018-12-12T10:25:57.890567522Z,
'd': [
{
't': 2018-12-12T10:25:57.890567522Z
'd': {
'wind-angle': 2.9,
'wind-spd': 1.44
},
{
't': 2018-12-12T10:25:57.918715188Z,
'd': {
'wind-angle': 285,
'wind-spd': 1.44
}
}
]
}
所需的JSON结构:
{
't': 1st timestamp from wind-angle,
'd': [
{
't': timestamp from wind-angle,
'd': {
'wind-angle': value_wind-angle,
'wind-spd': value_wind-spd
}
]
}
我看到的问题是,当我使用for
循环时,有5个数据点,而只有两个names
。因此,例如,以下循环逻辑:
main = {'t': 0, 'd':[]}
_d = {}
for each_dataset_time, each_dataset_val in results.raw['series'][0]['values']:
for each_series in results.raw['series']:
_d[each_series['name']] = each_dataset_val
_d['t'] = each_dataset_time
main['d'].append(_d)
即使满足JSON结构,我也会保持相同的值和时间戳。
如何清晰地遍历内部values
循环并确保dict将names
作为同一循环内所需结构的键?
答案 0 :(得分:1)
使用zip
生成d
听起来不错。
wind_angle = results.raw['series'][0]['values']
wind_spd = results.raw['series'][1]['values']
d = []
for wangle, wspd in zip(wind_angle, wind_spd):
inner = {}
inner['t'] = wangle[0]
inner['d'] = {'wind-angle': wangle[1], 'wind-spd': wspd[1]}
d.append(inner)