2
我想生成一個JSON文件,然後我可以使用D3等工具來理解這個新網絡元素的不同命令。帶有重複元素到JSON的文本文件
Here is a sample of how each command will look like.
樣品中的每一行是一個命令。爲每個命令分組得到一個類似於輸出格式的「樹」將是非常有用的。
我曾嘗試:
import sys, time, thread, re, commands, os, json
Extract = []
PL = ""
def DoIt(text,PL):
if not text.strip() == PL.strip():
if not text.strip() == "":
Extract.append(text.split())
PL = text.strip()
def ExecuteCmd(String):
(status,Data)=commands.getstatusoutput(String)
print ".",
PL = ""
LINES = open('pana', 'r').readlines() # Pana = https://en.wikipedia.org/wiki/Venezuelan_Spanish
for i in LINES:
DoIt(i,PL)
PL = i
ExecuteCmd("rm -rfv TreeTmp")
ExecuteCmd("mkdir TreeTmp")
for j in range((len(Extract))):
Command = ""
for i in range((len(Extract[j]))):
lASn = Extract[j][i]
MyStr = "<"
ReSearch = re.search(MyStr + "(.*)", lASn)
if ReSearch:
lASn = "NA-VA"
Command = Command + "/" + lASn
ExecuteCmd("mkdir TreeTmp" + Command)
def path_to_dict(path):
d = {'name': os.path.basename(path)}
if os.path.isdir(path):
d['children'] = [path_to_dict(os.path.join(path,x)) for x in os.listdir(path)]
else:
d['type'] = "file"
return d
with open('flare.json', 'w') as outfile:
json.dump(path_to_dict('./TreeTmp/'), outfile)
我已經做噸搜索在StackExchange,大概解析這個二進制樹,然後分析他們回來,或者創建數組在數組中的元素,或者步行過創建的目錄樹(上面的破壞代碼)。我在確定正確的邏輯方面確實存在問題。或者做到這一點的最佳方式。
謝謝,我會試一試 –