這是一個錯誤還是我錯過了什么?
我使用 for 回圈遍歷主題串列。在那個回圈中,我用另一個字典的值更新預制字典中的一個鍵。這部分作業正常。所以現在我給出了一個有效載荷字典,其中包含新的單位值和一個主題字串。但是最后一步,我想以主題為鍵撰寫一個新的 dict 并將整個有效負載作為值并沒有真正起作用,因為它總是更新 dict 的每個值,而不僅僅是我指向的值。
有誰知道我做錯了什么?謝謝!
代碼:
for topic in self.topics:
counter = 1
print(counter)
payload['unit'] = units[topic]
print(payload)
print(topic)
data[topic] = payload
print(data)
輸出:
1
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten1
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
2
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten2
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
3
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/RaumtemperaturFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
4
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/StromFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
5
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/SpannungFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
6
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/FrequenzFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
7
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/WirkleistungFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
8
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/BlindleistungFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
9
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/snmp-ausgesch.Service
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp':
'iso8601', 'unit': 'Vu'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/snmp-ausgesch.Service': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}
10
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Alarm-Ausgang01
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/snmp-ausgesch.Service': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Alarm-Ausgang01': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
11
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/T3-Bereit
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp':
'iso8601', 'unit': 'Vu'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/snmp-ausgesch.Service': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Alarm-Ausgang01': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/T3-Bereit': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}
uj5u.com熱心網友回復:
很難理解你需要什么。這是否為您提供了您期望的輸出?
topics = ['/test/mqtt/Hilfsknoten1', '/test/mqtt/Hilfsknoten2', '/test/mqtt/RaumtemperaturFP32']
payload = {'value': 'float', 'timestamp': 'iso8601', 'unit': True}
units = {'/test/mqtt/Hilfsknoten1': 'V', '/test/mqtt/Hilfsknoten2': 'Vu', '/test/mqtt/RaumtemperaturFP32': 'A'}
counter = 0
data = {}
for topic in topics:
counter = 1
print("\n")
print(counter)
payload_copy = payload.copy()
payload_copy['unit'] = units[topic]
print(payload_copy)
print(topic)
data[topic] = payload_copy
print(data)
最后輸出:3
{'value':'float','timestamp':'iso8601','unit':'A'} /test/mqtt/RaumtemperaturFP32 {'/test/mqtt/Hilfsknoten1':{'value':'float', '時間戳':'iso8601','unit':'V'},'/test/mqtt/Hilfsknoten2':{'value':'float','timestamp':'iso8601','unit':'Vu' }, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}}
uj5u.com熱心網友回復:
如果有人想在一個小例子上測驗它:
topics = ['/test/mqtt/Hilfsknoten1', '/test/mqtt/Hilfsknoten2', '/test/mqtt/RaumtemperaturFP32']
payload = {'value': 'float', 'timestamp': 'iso8601', 'unit': True}
units = {'/test/mqtt/Hilfsknoten1': 'V', '/test/mqtt/Hilfsknoten2': 'Vu', '/test/mqtt/RaumtemperaturFP32': 'A'}
counter = 0
data = {}
for topic in topics:
counter = 1
print("\n")
print(counter)
payload['unit'] = units[topic]
print(payload)
print(topic)
data[topic] = payload
print(data)
我得到了什么:
1
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten1
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
2
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/Hilfsknoten2
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}
3
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}
/test/mqtt/RaumtemperaturFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}}
我想要的是:
1
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten1
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}
2
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/Hilfsknoten2
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}
3
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}
/test/mqtt/RaumtemperaturFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/312970.html
