我有一个输入文件,格式如下
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
首席执行官在最高层,每个下属都应该在上级之下。所以不管第二个名字是什么,那就是主管。诀窍在于,如果一个员工有两个主管,他们需要与上面的直接主管进行两次“缩进”
我试过遍历列表,并通过“-”和逗号进行解析,但我正在努力解决结构本身。这就是我目前所拥有的
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)
我在这个问题上纠缠了很久,我想承认:(
很酷的问题,工作很有趣。我试着写得很透彻,结果有点长,我希望它还是可读的
代码:
输出:
相关问题 更多 >
编程相关推荐