-1
Hello, I have a csv file, from which I read to a dictionary, the file is as follows:
maquina,num,projeto,var,reg,nome,valor
br,1,1809123,float,4637,teste1
br,1,1809123,float,4639,teste2
br,2,1809123,float,4641,teste1
br,2,1809123,float,4643,teste2
br,2,1809123,int,4107,teste3
al,1,1809123,int,4186,teste4
al,2,1809123,float,4645,teste4
My dictionary looks like this(with the acquisition of values):
OrderedDict([('maquina', 'br'), ('num', '1'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4637'), ('nome', 'teste1'), ('valor', 1.1869791746139526)])
OrderedDict([('maquina', 'br'), ('num', '1'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4639'), ('nome', 'teste2'), ('valor', 0.1163330078125)])
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4641'), ('nome', 'teste1'), ('valor', 1077.43994140625)])
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4643'), ('nome', 'teste2'), ('valor', 124.00007629394531)])
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'int'), ('reg', '4107'), ('nome', 'teste3'), ('valor', 228)])
OrderedDict([('maquina', 'al'), ('num', '1'), ('projeto', '1809123'), ('var', 'int'), ('reg', '4186'), ('nome', 'teste4'), ('valor', 250)])
OrderedDict([('maquina', 'al'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4645'), ('nome', 'teste4'), ('valor', 0.0)])
Now I need to sort the data to later record it in a database, so I created the following function:
def preparar_dados(dados_modbus,maquina,num):
registradores = {}
for dado in dados_modbus:
if dado['maquina'] == maquina and dado['num'] == num:
registradores.update({dado['maquina']:dado['num'],dado['nome']:dado['valor']})
return registradores
In this function I provide the dictionary, the name of the machine and its number. But I’d like the show to do that without me having to tell him the name and one of the machine. I want a logic to change the function itself to not need to inform "host" and "one". The result of this new code would be the following:
{'al': '1', 'teste6': 0}
{'br': '1', 'teste1': 0.952343761920929, 'teste2': 0.0997314453125}
{'al': '2', 'teste7': 0.0}
{'br': '2', 'teste3': 1077.43994140625, 'teste4': 104.00007629394531, 'teste5': 0}
I look forward to some help in this matter.
What is the ordination rule?
– Woss
"machine" and "num" are the values that combined should be unique, at the moment I am informing them to the function manually, but I really wanted the program to do this automatically. I don’t know if ordering is the correct word, I need the function to return as a result the values I put at the end of the question. No need for this here:
dados_br1 = preparar_dados(dados_modbus,"br","1")
 dados_al1 = preparar_dados(dados_modbus,"al","1")
It should be something like this:dados_ordenados = preparar_dados(dados_modbus)
– JonP