大侠请留步!急急急!!!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)
[其他解释]
又是你。。。
明显你看逗号前面有个点的就是表示这个节点有子节点,再根据点的个数划分层数,很容易就得到你要的列表了
[其他解释]
既然是想用wxpython实现一个界面,试试创建一个wxTreeCtrl,逐行解析字串加入节点即可,另行转化成字典或列表,似乎没太大用途...
[其他解释]
我的代码没法满足你吗... 我觉得我理解你的需求了啊
[其他解释]
简单的说,可以使用:
json.dumps(root, indent=1);
即可实现输出树状的效果了。
复杂的解释,可以参考我专门写的教程:
【整理】Python中将(字典,列表等)变量格式化成(漂亮的,树形的,带缩进的,JSON方式的)字符串输出
[其他解释]
那使用json.dumps的对立函数json.loads,就可以把字符串,转换为对应的(字典,列表等)变量了。很好用的,你自己试试就知道了。
[其他解释]
求将数据处理成字典的过程
[其他解释]
如果节点有很多行,生成树的过程是不是很慢?
[其他解释]
我感觉lz是想将csv文件转换成xml文件。
------其他解决方案--------------------
亲,我是想把数据转成字典或列表呦!