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