0
i have a list of dictionaries that i want to process so that it matchs influxDB dict format:
dict={
"measurement": 'measurement_name",
"tags" : {"tag_key":"tag_value"},
"fields : {"field_name" : field_value}
}
my list of dicts:
list_val_machines=[
{"machine1_Data_variable1": value,
"machine1_Data_variable2" : value},
{"machine2_Data_variable1": value,
"machine1_Data_variable2" : value}
]
so here is what i tried so far:
for k,v in enumerate(list_val_machines): #loop on list items
for i,j in v.items(): #iterate on every key value of each dict
#list_machine_names=[]
#for k,v in raw_data_dict.items():
#list_machine_names.append(machine_name)
machine_name=i.split('_')[0] #parse machine name
variable_names=list(v.keys()) #get variable names (tag keys)
values_name=list(v.values()) #get values (field values)
for l,m in enumerate(variable_names):
for n,o in enumerate(values_name):
d={
"measurement" : machine_name,
"tags" : {"variable_name": m},
"fields" : {"variable_name": o}
}
list_d.append(d)
data=list_d
data variable looks like this:
for p in data:
print(p)
output:
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
measurement name is correct but i’m having the same tag field for every machine (exemple: machine1_Data_variable1) and same field value for every variable in a given machine. I don’t understand what is going wrong here. I want to have a 1 dict per variable with correct variable names and value.
Any hints on how to achieve this ?