我有以下 json 檔案
[
{
"v4-filter": {
"accept_to_test1t": {
"action": "accept"
},
"access_to_test2": [
{
"source-prefix-list": "x.x.x.x/yy"
},
{
"destination-prefix-list": "x.x.x.x/yy"
},
{
"action": "accept"
}
]
}
}
]
請注意,對于字典鍵accept_to_test1的值又是一個字典"action": "accept" 問題:我需要在串列中轉換所有嵌套字典,如上面的 case。在這種特定情況下,例如
accept_to_test1 : [ {"action": "accept"} ]。其余的都保持原樣。結果:
[
{
"v4-filter": {
"accept_to_test1t": [
{
"action": "accept"
}],
"access_to_test2": [
... like before ...
}
}
]
我正在使用 jinja2 模板來呈現這些資料,它可以作業,但僅適用于字典串列。
{%- for d in data_list -%}
{%- for k,v in d.items() -%}
{%- for term,value in v.items() -%}
term {{ term }} {{ '{ \n' }}
{%- set label = namespace(source=false, dest=false, port=false, action=false, prot=false) -%}
{%- for dict_item in value -%}
{%- for key, value in dict_item.items() -%}
{% if key == "source-prefix-list" %}
{% if not label.source %} source-address::{% else %}{{ ' '*18 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.source = true -%}
{% endif %}
{%- if key == "destination-prefix-list" -%}
{% if not label.dest %} destination-address::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.dest = true %}
{%- endif -%}
{%- if key == "destination-port" -%}
{% if not label.port %} destination-port::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.port = true -%}
{% endif %}
{%- if key == "action" -%}
{% if not label.action %} action::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.action = true -%}
{% endif %}
{%- if key == "protocol" -%}
{% if not label.protocol %} protocol::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.protocol = true -%}
{% endif %}
{%- endfor -%}
{%- endfor -%}
{{ '} \n\n' }}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
{# above template is for json file #}
渲染程序中出現問題
{%- for key, value in dict_item.items() -%}
jinja2.exceptions.UndefinedError: 'str object' has no attribute 'item
由于 {'action': 'accept'} 這不是可迭代項。它適用于 [{'action': 'accept'}] 之類的東西。
歡迎任何提示。
謝謝。
uj5u.com熱心網友回復:
這個問題的一個簡單解決方案是檢測value變數何時是一個字典,然后將它嵌入到一個串列中。對于檢測,我們可以使用mappingbultin 測驗。嵌入是一個簡單的封裝:{%- set value_list = [value] %}.
{%- for d in data_list -%}
{%- for k,v in d.items() -%}
{%- for term,value in v.items() -%}
term {{ term }} {{ '{ \n' }}
{%- set label = namespace(source=false, dest=false, port=false, action=false, prot=false) -%}
{%- if value is mapping() %}
{%- set value_list = [value] %}
{%- else %}
{%- set value_list = value %}
{%- endif %}
{%- for dict_item in value_list -%}
{%- for key, value in dict_item.items() -%}
{%- if key == "source-prefix-list" %}
{%- if not label.source %} source-address::{% else %}{{ ' '*18 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.source = true -%}
{%- endif %}
{%- if key == "destination-prefix-list" -%}
{%- if not label.dest %} destination-address::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.dest = true %}
{%- endif -%}
{%- if key == "destination-port" -%}
{%- if not label.port %} destination-port::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.port = true -%}
{%- endif %}
{%- if key == "action" -%}
{%- if not label.action %} action::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.action = true -%}
{%- endif %}
{%- if key == "protocol" -%}
{%- if not label.protocol %} protocol::{% else %}{{ ' '*20 }}{% endif %} {{value}} {{ '\n' }}
{%- set label.protocol = true -%}
{%- endif %}
{%- endfor -%}
{%- endfor -%}
{{ '} \n\n' }}
{%- endfor -%}
{%- endfor -%}
{%- endfor -%}
輸出:
term accept_to_test1t {
action:: accept
}
term access_to_test2 {
source-address:: x.x.x.x/yy
destination-address:: x.x.x.x/yy
action:: accept
}
我還修復了一些縮進問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/413395.html
標籤:
