簡體   English   中英

將包含鍵和值的列表轉換為 json 格式(python)

[英]convert list with keys & values to json format( python)

我正在使用的代碼

show_config = self.show_task(ha_task) show_config = show_ha_config[“任務”][0][“任務詳情”][0][“輸出”]

    # need to decode into base64
    show_decoded = base64.b64decode(
        bytes(show_config, "utf-8")
    ).decode("ascii")
    show_conf = show_decoded.split("\n")
    return show_conf
return show_conf = [
    '',
    'VSID:            0   ',
    'VRID:            0   ',
    'Type:            VSX Gateway',
    'Name:            chckpt-fw1a',
    'Security Policy: VS-policy',
    'Installed at:    12Jan2023 21:57:15',
    'SIC Status:      Trust',
    'Connections number: 52',
    'Connections peak: 152',
    'Connections limit:  14900',
]

我正在使用下面的代碼將其轉換為 json 但響應不好

json_format = json.dumps(show_conf)
return json_format

json_format 我得到:

["", "VSID:            0   ", '
                                  '"VRID:            0   ", "Type:            '
                                  'VSX Gateway", "Name:            '
                                      'chckpt-fw1a", "Security Policy: '
                                      'VS-policy", "Installed at:    '
                                      '12Jan2023 21:57:15", "SIC Status:      '
                                      'Trust", "Connections number: 52", '
                                      '"Connections peak:   152", "Connections '
                                      'limit:  14900",]

我需要的 json_format:

['', 
'"VSID": "0"   ',
'"VRID": "0"   ',
'Type:   "VSX Gateway"',
'"Name": "chckpt-fw1a"',
'"Security Policy": "VS-policy"',
"Installed at":    "12Jan2023 21:57:15"',
"SIC Status":      "Trust"',
"Connections number": "52"',
"Connections peak":   "152"',
"Connections limit":  "14900"',]

您可能首先要將此列表轉換為字典 為此,我們首先需要解析其中的每一項,然后將其全部轉儲到一個字符串/文件中。

import json  # Python built-in json parser

# Raw data
output = [
    '',
    'VSID:            0   ',
    'VRID:            0   ',
    'Type:            VSX Gateway',
    'Name:            chckpt-fw1a',
    'Security Policy: VS-policy',
    'Installed at:    12Jan2023 21:57:15',
    'SIC Status:      Trust',
    'Connections number: 52',
    'Connections peak: 152',
    'Connections limit:  14900',
]

# Declaring the dictionary for output
output_dict = {}

# Iterating through each item of `output` list
for item in output:
    # Lets take this item as sample - 'VSID:            0   '

    # Split the string by `: ` separator to list. Separator WILL be removed.
    # Result will look like this -
    #     ['VSID', '           0   ']
    splitted = item.split(": ")

    # Skip empty items or single keys
    if len(splitted) <= 1:
        continue

    output_dict[
        splitted[0].strip()  # Key - 'VSID'
    ] = splitted[1].strip()  # Value - '0'
    # `.strip()` will remove all the trailing and leading whitespaces

# Dump the generated dictionary to this variable as string.
# `indent=4` will generate the output in more human-readable format.
jsn = json.dumps(output_dict, indent=4)

# Display the json
print(jsn)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM