作爲一名java開發人員,我需要一些提示如何在python 2中解決這個問題。我的python技能處於開始狀態。但現在的問題是:使用點分隔字符串轉換爲嵌套複雜字典
我們爲設備提供服務,設備以某種格式報告一些技術統計數據,我們無法更改。服務器使用python運行。 主要的報告是作爲字典來的,我們需要保存json的方式。從字典到json的轉換不是問題,但是轉換平面和用點分隔的鍵需要轉換。
也許一個例子可以顯示我想說的。這是來自設備的格式,將其命名爲:
{
'Device.DeviceInfo.SoftwareVersion': 'ote-2.2.1',
'Device.GatewayInfo.ProductClass': 'OEM-TX23',
'Device.GatewayInfo.SerialNumber': 'A223142D1CC7',
'Device.Ethernet.Interface.1.MaxBitRate': 1000,
'Device.HomePlug.Interface.1.AssociatedDevice.1.RxPhyRate': 522,
'Device.HomePlug.Interface.1.AssociatedDevice.1.TxPhyRate': 706,
'Device.HomePlug.Interface.1.AssociatedDevice.1.Active': 1,
'Device.HomePlug.Interface.1.AssociatedDevice.1.MACAddress': 'af:49:79:e4:64:fc',
'Device.HomePlug.Interface.1.AssociatedDevice.2.RxPhyRate': 544,
'Device.HomePlug.Interface.1.AssociatedDevice.2.TxPhyRate': 0,
'Device.HomePlug.Interface.1.AssociatedDevice.2.Active': 1,
'Device.HomePlug.Interface.1.AssociatedDevice.2.MACAddress': 'af:49:79:e4:64:dd',
'Device.Ethernet.Interface.2.MaxBitRate': 1000,
'Device.HomePlug.Interface.2.AssociatedDevice.1.RxPhyRate': 671,
'Device.HomePlug.Interface.2.AssociatedDevice.1.TxPhyRate': 607,
'Device.HomePlug.Interface.2.AssociatedDevice.1.Active': 1,
'Device.HomePlug.Interface.2.AssociatedDevice.1.MACAddress': 'bf:49:79:e4:64:fc',
'Device.HomePlug.Interface.2.AssociatedDevice.2.RxPhyRate': 340,
'Device.HomePlug.Interface.2.AssociatedDevice.2.TxPhyRate': 0,
'Device.HomePlug.Interface.2.AssociatedDevice.2.Active': 1,
'Device.HomePlug.Interface.2.AssociatedDevice.2.MACAddress': 'bf:49:79:e4:64:dd'
}
源中的整數值表示此接口的接口和AssociatedDevices的索引。所以整數後面的部分應該是多個詞典列表。結果中不應包含整數值。
我們需要下面的嵌套結構,然後才能將其保存到數據庫,尤其是mysql文檔庫。再次,從嵌套字典到json的轉換不是問題。
這是我們需要的格式:
{
'Device': {
'GatewayInfo': {
'SerialNumber': 'A223142D1CC7',
'ProductClass': 'OEM-TX23'
},
'DeviceInfo': {
'SoftwareVersion': 'ote-2.2.1'
},
'Ethernet': {
'Interface': [{
'MaxBitRate': 1000
}, {
'MaxBitRate': 1000
}]
},
'HomePlug': {
'Interface': [{
'AssociatedDevice': [{
'RxPhyRate': 522,
'TxPhyRate': 706,
'Active': 1,
'MACAddress': 'af:49:79:e4:64:fc',
}, {
'RxPhyRate': 544,
'TxPhyRate': 0,
'Active': 1,
'MACAddress': 'af:49:79:e4:64:dd',
}]
}, {
'AssociatedDevice': [{
'RxPhyRate': 671,
'TxPhyRate': 607,
'Active': 1,
'MACAddress': 'bf:49:79:e4:64:fc',
}, {
'RxPhyRate': 340,
'TxPhyRate': 0,
'Active': 1,
'MACAddress': 'bf:49:79:e4:64:dd',
}]
}]
}
}
}
UPDATE: 第一個答案是部分正確的,只是整數後的部分應轉化成含有其餘的作爲字典的列表。
並不多,遍歷列表並嘗試瞭解python中的不同數據結構。但我的問題是,python對我來說是新的。 – AdemC