如何在jinja2 temp中使用json格式

2024-04-20 13:42:18 发布

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

我已经从.xlsx获取了json格式,现在我想将json用于jinja2模板,但是我不知道如何使用它来获得所需的输出。你知道吗

这是我的json:

{'Nodeb_IN_New': [{'policy_id': 107, 'default': 'class-default', 'class': 'mobility-platinum', 'mpls': 'h1', 'qos': 7, 'nokia': 'dscp-fc-map', 'dscp': ['ef']}, {'policy_id': 107, 'default': '', 'class': 'mobility-gold-new', 'mpls': 'h2', 'qos': 5, 'nokia': 'dscp-fc-map', 'dscp': ['af41']}, {'policy_id': 107, 'default': '', 'class': 'mobility-silver-new', 'mpls': 'l1', 'qos': 4, 'nokia': 'dscp-fc-map', 'dscp': ['af11', 'af21', 'af31']}, 'Nokia_SRAN_S1-MME_X2_IN':[{'policy_id': 102, 'default': '', 'class': 'Nokia_SRAN_mobility_platinum', 'mpls': 'h1', 'qos': 7, 'nokia': 'dscp-fc-map', 'dscp': ['ef', 'nc1']}]}

生成json的代码:

import json

# Jinja Part
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)

# Port config Class
qos_template = env.get_template('jinjaQosTemplate3.txt')

def myfun(list):
    res=dict()
    qos=None
    d={}
    li=[]
    for row in list:
        if row[0]=="":
            res[qos]=dict()
            res[qos]['policy_id']=row[1]
            res[qos]['default']=row[2]
            res[qos]['class']=row[3]
            res[qos]['mpls']=row[4]
            res[qos]['qos']=row[5]
            res[qos]['nokia']=row[6]
            res[qos]['dscp']=row[7]
        else:
            qos=row[0]
            res[qos]=dict()
            res[qos]['policy_id']=row[1]
            res[qos]['default']=row[2]
            res[qos]['class']=row[3]
            res[qos]['mpls']=row[4]
            res[qos]['qos']=row[5]
            res[qos]['nokia']=row[6]
            res[qos]['dscp']=row[7]
        x = res.keys()
        keylist = []
        keylist.extend(iter(x))
        if keylist[0] in d.keys():
               d[keylist[0]].append(res[qos])
        else:
               d[keylist[0]] = []
               d[keylist[0]].append(res[qos])
    print(d)
    print(json.dumps(d))

    output = qos_template.render(data=d,zip=zip)
    file1.writelines(output)
    file1.writelines('\n')

我想使用json获得所需的输出,如下所示:

/configure qos
        dscp-fc-map 100 create
            description "Nokia_SRAN_S1-MME_X2_IN"
            dscp ef fc "h1"
            dscp nc1 fc "h1"
        exit


        ingress-cos-policy 100 create
            description "Nokia_SRAN_S1-MME_X2_IN"
            enable-dscp-exp-remarking
            dscp-fc-map 100
        exit

        sap-ingress 100 create
            description "Nokia_SRAN_S1-MME_X2_IN"
            ingress-cos-policy 100
            policer 7 create 
            exit
            fc "h1" create
                policer 7
            exit
        exit

-----------------
/configure qos
        dscp-fc-map 101 create
            description "Nodeb_IN_New"
            dscp ef fc "h1"
            dscp af11 fc "l1"
            dscp af21 fc "l1"
            dscp af31 fc "l1"
            dscp af41 fc "h2"
            default-fc be   
        exit

        ingress-cos-policy 101 create
            description "Nodeb_IN_New"
            enable-dscp-exp-remarking
            dscp-fc-map 101
        exit


        sap-ingress 101 create
            description "Nodeb_IN_New"
            ingress-cos-policy 101
            policer 1 create
            exit
            policer 4 create
            exit
            policer 5 create
            exit
            policer 7 create
            exit
            fc "be" create
                policer 1
            exit
            fc "h1" create
                policer 7
            exit
            fc "h2" create
                policer 5
            exit
            fc "l1" create
                policer 4
            exit
        exit

我试图使这个jinja模板,但我认为这是错误的,所以请建议如何使我的模板得到想要的输出。你知道吗

/configure qos 
{%-for k,v in data.items()%}
    {%-if (v['nokia'])|length>0%}
    {{v['nokia']}} {{v['policy_id']}} create
            description "{{k}}"
            {%-for i in range(0,(v['dscp'])|length)%}
            dscp {{v['dscp'][i]}} fc "{{v['mpls']}}"
            {%endfor%}
        exit
    {%endif%}
        ingress-cos-policy {{v['policy_id']}} create
            description "{{k}}"
            enable-dscp-exp-remarking
            {{v['nokia']}} {{v['policy_id']}}
        exit

        sap-ingress {{v['policy_id']}} create
            description "{{k}}"
            ingress-cos-policy {{v['policy_id']}}
    {%-for j in queue%}
            policer {{queue}} create
            exit
    {%-endfor%}
    {%-for j,l in zip(queue,v['dscp'])%}
            fc "{{v['dscp']}}" create
                policer {{queue}}
            exit
    {%-endfor%}
        exit           
{%-endfor%}

我得到的结果是这样的:

----------------------------------------------
echo "QOS Configuration"
----------------------------------------------

        ingress-cos-policy  create
            description "Nodeb_IN_New"
            enable-dscp-exp-remarking

        exit

        sap-ingress  create
            description "Nodeb_IN_New"
            ingress-cos-policy 
        exit

Tags: iniddefaultcreatepolicyexitresdescription