簡體   English   中英

缺少幾行打印

[英]missing few lines to print

下面是我的代碼:

output_dict = {}
for item in show_vs:
    splitted = item.split(": ")
    if len(splitted) <= 1:
        continue
    output_dict[
        splitted[0].strip()
    ] = splitted[1].strip()
jsn = json.dumps(output_dict, indent=4)
print(jsn)

下面是我的數據,該代碼僅以 json 格式打印最后 10 個鍵及其值,但根本不打印前 20 個鍵及其值,有什么建議嗎?

show_vs = [
    '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',

    'VSID:            1   ',
    'VRID:            1   ',
    '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',

    'VSID:            2   ',
    'VRID:            2   ',
    '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',
]

字典中不能有重復的鍵,因此您需要創建一個字典列表,每個 VSID 一個。

output_dicts = []

for  item in show_vs:
    splitted = item.split(": ")
    if len(splitted) <= 1:
        continue
    key, value = map(str.strip, splitted[:2])
    if key == 'VSID':
        new_dict = {}
        output_dicts.append(new_dict)
    new_dict[key] = value

jsn = json.dumps(output_dicts, indent=4)
print(jsn)

暫無
暫無

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

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