0
我有一个输入文件,格式如下。试图从列表中构建组织树
Fred,Karl,Technician,2010--Karl,Cathy,VP,2009--Cathy,NULL,CEO,2007--
--Vince,Cathy,Technician,2010
我需要解析这些信息到它结束了看起来像这样的一个输出文件:
Cathy (CEO) 2007
-Karl (VP) 2009
--Fred (Technician) 2010
-Vince (Technician) 2010
在顶部的CEO,每个下属应当根据自己的优越。所以不管第二个名字是什么,那就是主管。诀窍是,如果一名员工有两名主管,他们需要两次缩进“ - ”与他们的直属主管。
我已经尝试遍历列表并解析通过“ - ”和逗号,但我正在努力与结构本身。这是我迄今为止所拥有的。
with open('org_chart_sample.in', 'r') as reader: # Open the input file
with open('output.out', 'w') as writer: # Make output file writable
reader.readline() # Ignore first line
lines = reader.readlines() # Read input lines
for line in lines: # Parse out input by the -- which separated attributes of people in the org
employees = line.split('--')
hierarchy = [] # Exterior list to aid in hierarchy
for employee in employees: # Logic that adds to the hierarchy list as algorithm runs
info = employee.split(',')
hierarchy.append(info)
我一直停留在这个问题上不再是那个喜欢我承认:(
你能完成这个例子吗? nancy和bob合适? – mitoRibo
我的歉意@rbierman,我修正了这个例子 – ozbrancov