将包含重复元素的文本文件转换为JSON

2024-09-29 19:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想生成一个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中做了大量的搜索,可能是将其解析为一个二叉树,然后再将它们解析回来,或者在数组上创建数组元素,或者遍历创建的目录树(上面的断开的代码)。我真的很难找到正确的逻辑。或者最好的方法。你知道吗


Tags: pathtextin命令jsonforifos
1条回答
网友
1楼 · 发布于 2024-09-29 19:30:34

如果您只寻找唯一的,并且顺序无关紧要,那么您几乎可以不费吹灰之力就用Python中的dictionareofdictionares获得一个很好的树表示。你知道吗

def add_items(tree, items):
    if items[0] not in tree:
        branch = {}
        tree[items[0]] = branch
    else:
        branch = tree[items[0]]
    if len(items) > 1:
        add_items(branch, items[1:])    

tree = {}
with open('commands.txt') as f:
    for line in f:
        items = line.strip().split()
        add_items(tree, items)

from pprint import pprint
pprint(tree)

输出:

{'param-1': {'param-2': {'param-X': {'gateway': {'<name>': {'protocol': {'param-Xv1': {'dpd': {}}},
                                                            'protocol-common': {'fragmentation': {},
                                                                                'nat-traversal': {}}}},
                                     'param-Z': {'ipsec-param-Z': {'<name>': {'ah': {'authentication': {}},
                                                                              'esp': {'authentication': {},
                                                                                      'encryption': {}},
                                                                              'lifesize': {},
                                                                              'lifetime': {}}},
                                                 'param-X-param-Z': {'<name>': {'A': {},
                                                                                'B': {},
                                                                                'C': {},
                                                                                'D': {}}}}}},
             'param-3': {'param-3': {'ntp-servers': {},
                                     'param-5': {'param-6': {}},
                                     'update-schedule': {'global-protect-datafile': {'recurring': {'weekly': {}}}}}}}}

如果必须使用问题中指定的格式,并带有name和children标记,则应该可以:

class Element(object):
    def __init__(self, name, children=None):
        self.name = name    
        self.children = [] if children is None else children
    def to_dict(self):
        d = {'name': self.name}
        if self.children:
            d['children'] = [c.to_dict() for c in self.children]
        return d

def add_items(children, items):
    head, *tail = items
    for child in children:
        if child.name == head:
            break
    else:
        child = Element(head)
        children.append(child)
    if tail:
        add_items(child.children, tail)

root = Element('root')

with open('commands.txt') as f:
    for line in f:
        items = line.strip().split()
        add_items(root.children, items)

输出:

{'children': [{'children': [{'children': [{'children': [{'children': [{'name': 'param-6'}],
         'name': 'param-5'},
        {'name': 'ntp-servers'},
        {'children': [{'children': [{'children': [{'name': 'weekly'}],
             'name': 'recurring'}],
           'name': 'global-protect-datafile'}],
         'name': 'update-schedule'}],
       'name': 'param-3'}],
     'name': 'param-3'},
    {'children': [{'children': [{'children': [{'children': [{'children': [{'children': [{'name': 'dpd'}],
               'name': 'param-Xv1'}],
             'name': 'protocol'},
            {'children': [{'name': 'nat-traversal'},
              {'name': 'fragmentation'}],
             'name': 'protocol-common'}],
           'name': '<name>'}],
         'name': 'gateway'},
        {'children': [{'children': [{'children': [{'name': 'A'},
              {'name': 'B'},
              {'name': 'C'},
              {'name': 'D'}],
             'name': '<name>'}],
           'name': 'param-X-param-Z'},
          {'children': [{'children': [{'children': [{'name': 'encryption'},
                {'name': 'authentication'}],
               'name': 'esp'},
              {'children': [{'name': 'authentication'}], 'name': 'ah'},
              {'name': 'lifetime'},
              {'name': 'lifesize'}],
             'name': '<name>'}],
           'name': 'ipsec-param-Z'}],
         'name': 'param-Z'}],
       'name': 'param-X'}],
     'name': 'param-2'}],
   'name': 'param-1'}],
 'name': 'root'}

相关问题 更多 >

    热门问题