我想生成一个JSON文件,在其中我可以使用D3等工具来理解这个新的网络元素的不同命令。你知道吗
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中做了大量的搜索,可能是将其解析为一个二叉树,然后再将它们解析回来,或者在数组上创建数组元素,或者遍历创建的目录树(上面的断开的代码)。我真的很难找到正确的逻辑。或者最好的方法。你知道吗
如果您只寻找唯一的,并且顺序无关紧要,那么您几乎可以不费吹灰之力就用Python中的dictionareofdictionares获得一个很好的树表示。你知道吗
输出:
如果必须使用问题中指定的格式,并带有name和children标记,则应该可以:
输出:
相关问题 更多 >
编程相关推荐