私は次の形式の入力ファイルを持っています。私はそれが出力ファイルに次のようなものを見終わるところに、この情報を解析する必要がリストから組織ツリーを構築しようとしています
Fred,Karl,Technician,2010--Karl,Cathy,VP,2009--Cathy,NULL,CEO,2007--
--Vince,Cathy,Technician,2010
:上部のCEOで
Cathy (CEO) 2007
-Karl (VP) 2009
--Fred (Technician) 2010
-Vince (Technician) 2010
を、各下位はその優れ下でなければなりません。二番目の名前が何であれ、それは上司です。そのトリックは、従業員が2人の監督者を抱えている場合、上記の直属上司と2回 " - "字下げする必要があるということです。
私はリストを反復処理し、 " - "とコンマで解析しようとしましたが、構造自体に苦労しています。これは私がこれまで持っていたものです。
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)
私は:(
例を完了できますか?ナンシーとボブはどこに合う? – mitoRibo
私の謝罪@rbierman、私は例を修正しました – ozbrancov