就像之前已經提到的str.lstrip()
不是一個增變器,索引在我的系統中也很準確。
但問題在於,當你意識到該行的索引增加時,line
實際上指向增加的索引行,例如,在第一種情況下,我們注意到行的索引增加爲line ba
,所以line
點line ba
,然後在你的if
情況下,你做 -
ret[line.strip()] = parse_message_to_tree_helper(buf, index)
這是錯誤的,因爲你會被設置無論是通過parse_message_to_tree_helper()
返回line ba
,而不是其實際的父。另外,一旦你在函數內部遞進,除非文件已被完全讀取,否則你不會出來,但是在字典中存儲某一行的級別取決於當縮進減少時遞歸出來的級別。
我不知道,如果有,我能想出(基於大量的代碼)的任何內置庫,這將幫助你做到這一點,但一碼 -
def parse_message_to_tree(message):
buf = StringIO(message)
return parse_message_to_tree_helper(buf, 0, None)[0]
def parse_message_to_tree_helper(buf, prev, prevline):
ret = {}
index = -1
for line in buf:
line = line.rstrip()
index = len(line) - len(line.lstrip())
print (line + " => " + str(index))
if index > prev:
ret[prevline.strip()],prevline,index = parse_message_to_tree_helper(buf, index, line)
if index < prev:
return ret,prevline,index
continue
elif not prevline:
ret[line.strip()] = {}
else:
ret[prevline.strip()] = {}
if index < prev:
return ret,line,index
prevline = line
if index == -1:
ret[prevline.strip()] = {}
return ret,None,index
if prev == index:
ret[prevline.strip()] = {}
return ret,None,0
示例/演示 -
>>> print(s)
line a
line b
line ba
line bb
line bba
line bc
line c
line ca
line caa
>>> def parse_message_to_tree(message):
... buf = StringIO(message)
... return parse_message_to_tree_helper(buf, 0, None)[0]
...
>>> def parse_message_to_tree_helper(buf, prev, prevline):
... ret = {}
... index = -1
... for line in buf:
... line = line.rstrip()
... index = len(line) - len(line.lstrip())
... print (line + " => " + str(index))
... if index > prev:
... ret[prevline.strip()],prevline,index = parse_message_to_tree_helper(buf, index, line)
... if index < prev:
... return ret,prevline,index
... continue
... elif not prevline:
... ret[line.strip()] = {}
... else:
... ret[prevline.strip()] = {}
... if index < prev:
... return ret,line,index
... prevline = line
... if index == -1:
... ret[prevline.strip()] = {}
... return ret,None,index
... if prev == index:
... ret[prevline.strip()] = {}
... return ret,None,0
...
>>> pprint.pprint(parse_message_to_tree(s))
line a => 0
line b => 0
line ba => 2
line bb => 2
line bba => 4
line bc => 2
line c => 0
line ca => 2
line caa => 4
{'line a': {},
'line b': {'line ba': {}, 'line bb': {'line bba': {}}, 'line bc': {}},
'line c': {'line ca': {'line caa': {}}}}
>>> s = """line a
... line b
... line ba
... line bb
... line bba
... line bc
... line c
... line ca
... line caa
... line d"""
>>> pprint.pprint(parse_message_to_tree(s))
line a => 0
line b => 0
line ba => 2
line bb => 2
line bba => 4
line bc => 2
line c => 0
line ca => 2
line caa => 4
line d => 0
{'line a': {},
'line b': {'line ba': {}, 'line bb': {'line bba': {}}, 'line bc': {}},
'line c': {'line ca': {'line caa': {}}},
'line d': {}}
您將需要測試的代碼,任何更多的錯誤或遺漏的一些情況。
你見過autovivifyingating樹hack了嗎?可以爲你節省一些擊鍵:'tree = lambda:defaultdict(tree); t = tree(); t ['a'] ['b'] ['c'] =「bla」' –