大侠请留步!急急急!!!python将数据处理成树
拜托了~~~~
需求:将下面的数据生成一个树结构,
“root”是根节点
“.” 后的数据表示子节点
“,”后边的表示叶子节点的值
希望用wxpython实现一个界面,点击按钮弹出这个树结构,叶子节点的值可以先不显示,把“,”前面的字符串处理成树即可。
数据如下:
root.,0
root.DeviceSummary,0
root.LANDeviceNumberOfEntries,0
root.WANDeviceNumberOfEntries,0
root.DeviceInfo.,0
root.DeviceInfo.Manufacturer,0
root.DeviceInfo.ManufacturerOUI,0
root.DeviceInfo.ModelName,0
root.DeviceInfo.Description,0
root.DeviceInfo.ProductClass,0
root.DeviceInfo.SerialNumber,0
root.DeviceInfo.HardwareVersion,0
root.DeviceInfo.SoftwareVersion,0
root.DeviceInfo.SpecVersion,0
root.DeviceInfo.ProvisioningCode,0
root.DeviceInfo.DeviceStatus,0
root.DeviceInfo.UpTime,0
root.DeviceInfo.DeviceLog,0
root.DeviceInfo.X_CT-COM_LoadInfo.ProcessorLoad,0
root.DeviceInfo.X_CT-COM_LoadInfo.MemoryLoad,0
root.DeviceInfo.X_CT-COM_Alarm.,0
root.DeviceInfo.X_CT-COM_Alarm.Enable,1
root.DeviceInfo.X_CT-COM_Alarm.AlarmNumberOfEntries,0
root.DeviceInfo.X_CT-COM_Alarm.AlarmConfig.,1
root.DeviceInfo.X_CT-COM_ServiceManage.,0
root.DeviceInfo.X_CT-COM_ServiceManage.FtpEnable,1
root.DeviceInfo.X_CT-COM_ServiceManage.FtpUserName,1
root.DeviceInfo.X_CT-COM_ServiceManage.FtpPassword,1
root.DeviceInfo.X_CT-COM_ServiceManage.FtpPort,1
root.DeviceInfo.X_CT-COM_ServiceManage.Enable,1
root.DeviceInfo.X_CT-COM_ServiceManage.AccessControl,1
root.DeviceInfo.X_CT-COM_ServiceManage.TrustHost,1
... ...
列表:
root = [
"DeviceSummary",
"LANDeviceNumberOfEntries",
"WANDeviceNumberOfEntries",
["DeviceInfo",[
"Manufacturer",
"ManufacturerOUI",
"ModelName",
"Description",
"ProductClass",
"SerialNumber",
"HardwareVersion",
"SoftwareVersion",
"SpecVersion",
"ProvisioningCode",
"DeviceStatus",
"UpTime",
"DeviceLog",
["X_CT-COM_LoadInfo",[
"ProcessorLoad",
"MemoryLoad"]
"X_CT-COM_Alarm",[
"Enable",
"AlarmNumberOfEntries",
"AlarmConfig"]
"X_CT-COM_ServiceManage",[
"FtpEnable",
"FtpUserName",
"FtpPassword",
"FtpPort",
"Enable",
"AccessControl",
"TrustHost"]]]]
[root@vps616 python]# python main.py
{ 'root': { 'child': { 'DeviceInfo': { 'child': { 'Description': { 'child': { },
'val': '0'},
'DeviceLog': { 'child': { },
'val': '0'},
'DeviceStatus': { 'child': { },
'val': '0'},
'HardwareVersion': { 'child': { },
'val': '0'},
'Manufacturer': { 'child': { },
'val': '0'},
'ManufacturerOUI': { 'child': { },
'val': '0'},
'ModelName': { 'child': { },
'val': '0'},
'ProductClass': { 'child': { },
'val': '0'},
'ProvisioningCode': { 'child': { },
'val': '0'},
'SerialNumber': { 'child': { },
'val': '0'},
'SoftwareVersion': { 'child': { },
'val': '0'},
'SpecVersion': { 'child': { },
'val': '0'},
'UpTime': { 'child': { },
'val': '0'},
'X_CT-COM_Alarm': { 'child': { 'AlarmConfig': { 'child': { },
'val': '1'},
'AlarmNumberOfEntries': { 'child': { },
'val': '0'},
'Enable': { 'child': { },
'val': '1'}},
'val': '0'},
'X_CT-COM_LoadInfo': { 'child': { 'MemoryLoad': { 'child': { },
'val': '0'},
'ProcessorLoad': { 'child': { },
'val': '0'}},
'val': None},
'X_CT-COM_ServiceManage': { 'child': { 'AccessControl': { 'child': { },
'val': '1'},
'Enable': { 'child': { },
'val': '1'},
'FtpEnable': { 'child': { },
'val': '1'},
'FtpPassword': { 'child': { },
'val': '1'},
'FtpPort': { 'child': { },
'val': '1'},
'FtpUserName': { 'child': { },
'val': '1'},
'TrustHost': { 'child': { },
'val': '1'}},
'val': '0'}},
'val': '0'},
'DeviceSummary': { 'child': { }, 'val': '0'},
'LANDeviceNumberOfEntries': { 'child': { },
'val': '0'},
'WANDeviceNumberOfEntries': { 'child': { },
'val': '0'}},
'val': '0'}}
[root@vps616 python]# cat main.py
#python2.7.3
#coding=utf-8
class Tree:
def __init__(self):
self.m_tree = {}
def Feed(self, rows):
for row in rows:
path, val = row.split(',')
nodes = [node for node in path.split('.') if node.strip()]
num = len(nodes)
cur_node = self.m_tree
for i in range(0, num):
if nodes[i] not in cur_node:
cur_node[nodes[i]] = {'val': None, 'child': {}}
if i == num - 1:
cur_node[nodes[i]]['val'] = val
cur_node = cur_node[nodes[i]]['child']
tree = Tree()
strm = [line.strip() for line in open('text', 'r')]
tree.Feed(strm)
import pprint
pp = pprint.PrettyPrinter(indent = 4)
pp.pprint(tree.m_tree)
json.dumps(root, indent=1);