2017-08-28 120 views
-1

我需要在python中將他輸出轉換爲Json格式。在Python中將表格CLI輸出轉換爲JSON格式

我該怎麼辦?

switch# sh mod 
Mod Ports Module-Type       Model    Status 
--- ----- ----------------------------------- ------------------ ---------- 
1 48  1/2/4/8 Gbps FC/Supervisor-3  DS-C9148-K9-SUP active * 

Mod Sw    Hw  World-Wide-Name(s) (WWN) 
--- -------------- ------ -------------------------------------------------- 
1 6.2(17)   1.1  20:01:54:7f:ee:df:88:f8 to 20:30:54:7f:ee:df:88:f8 


Mod MAC-Address(es)       Serial-Num 
--- -------------------------------------- ---------- 
1 c0-8c-60-65-82-dc to c0-8c-60-65-82-df JAF1736ALLM 

輸入1:https://i.stack.imgur.com/EGsY4.jpg

輸入2:https://i.stack.imgur.com/aDGcB.jpg

+3

1 。輸出應該是什麼樣子,以及2.你嘗試過的什麼都不起作用? –

+1

我想說你必須使用複雜的常規ex或有狀態行解析器。不幸的是,兩者都會處於挑戰和醜陋之間。 –

回答

0

您可以使用'---'分隔符來定義每個鍵和值行的切片以構建每個鍵值。 (從你的榜樣,我猜有多個「國防部」,與時俱進的獨特國防部的價值觀,所以我用這個領域的整體蓄電池鍵。)

from collections import defaultdict 
import re 
from itertools import groupby 

sample = """\ 
Mod Ports Module-Type       Model    Status 
--- ----- ----------------------------------- ------------------ ---------- 
1 48  1/2/4/8 Gbps FC/Supervisor-3  DS-C9148-K9-SUP active * 
2 48  1/2/4/8 Gbps FC/Supervisor-3  DS-C9148-K9-SUP active * 

Mod Sw    Hw  World-Wide-Name(s) (WWN) 
--- -------------- ------ -------------------------------------------------- 
1 6.2(17)   1.1  20:01:54:7f:ee:df:88:f8 to 20:30:54:7f:ee:df:88:f8 
2 6.2(17)   1.1  20:01:54:7f:ee:df:88:f8 to 20:30:54:7f:ee:df:88:f8 

Mod MAC-Address(es)       Serial-Num 
--- -------------------------------------- ---------- 
1 c0-8c-60-65-82-dc to c0-8c-60-65-82-df JAF1736ALLM 
2 c0-8c-60-65-82-ec to c0-8c-60-65-82-ef JAF1736AXXX 

Xbar Ports Module-Type Model Status 
---- ----- ----------- ----- ------ 
1 0  Fabric 1 ABC ok 

Xbar Sw Hw 
---- -- --- 
1 NA 1.0 

""" 

all_input_lines = sample.splitlines() 
mod_accum = defaultdict(dict) 
xbar_accum = defaultdict(dict) 

for is_blank, input_lines_iter in groupby(all_input_lines, 
              key=lambda s: not bool(s.strip())): 
    input_lines = list(input_lines_iter) 
    if is_blank: 
     continue 

    # assume first two lines are field names and separator dashes 
    names, dashes = input_lines[:2] 

    # make sure dashes line is all '---' separators 
    if not all(ss == set('-') for ss in map(set, dashes.split())): 
     print("invalid line group found, skipping...") 
     print('-'*40) 
     print('\n'.join(input_lines)) 
     print('-'*40) 
     continue 

    # use regex to get start/end of each '---' divider, and make slices 
    spans = (match.span() for match in re.finditer('-+', dashes)) 
    slices = [slice(sp[0], sp[1]+1) for sp in spans] 

    names = [names[sl].rstrip() for sl in slices] 

    # is this a module or an xbar? 
    if 'Mod' in names: 
     key = 'Mod' 
     accum = mod_accum 
    elif 'Xbar' in names: 
     key = 'Xbar' 
     accum = xbar_accum 
    else: 
     raise ValueError("no Mod or Xbar name in row names ({})".format(
          ",".join(names))) 

    for line in input_lines: 
     # use slices to extract data from values, make into a dict 
     row_dict = dict(zip(names, (line[sl].rstrip() for sl in slices))) 

     # accumulate these values into any previous ones collected for this Mod 
     accum[row_dict[key]].update(row_dict) 

# print out what we got 
import json 
all_data = {"Modules": mod_accum, "Xbars": xbar_accum} 
print(json.dumps(all_data, indent=2)) 

打印:

{ 
    "Modules": { 
    "2": { 
     "World-Wide-Name(s) (WWN)": "20:01:54:7f:ee:df:88:f8 to 20:30:54:7f:ee:df:88:f8", 
     "Module-Type": "1/2/4/8 Gbps FC/Supervisor-3", 
     "Ports": "48", 
     "Sw": "6.2(17)", 
     "Hw": "1.1", 
     "Model": "DS-C9148-K9-SUP", 
     "Status": "active *", 
     "Serial-Num": "JAF1736AXXX", 
     "MAC-Address(es)": "c0-8c-60-65-82-ec to c0-8c-60-65-82-ef", 
     "Mod": "2" 
    }, 
    "1": { 
     "World-Wide-Name(s) (WWN)": "20:01:54:7f:ee:df:88:f8 to 20:30:54:7f:ee:df:88:f8", 
     "Module-Type": "1/2/4/8 Gbps FC/Supervisor-3", 
     "Ports": "48", 
     "Sw": "6.2(17)", 
     "Hw": "1.1", 
     "Model": "DS-C9148-K9-SUP", 
     "Status": "active *", 
     "Serial-Num": "JAF1736ALLM", 
     "MAC-Address(es)": "c0-8c-60-65-82-dc to c0-8c-60-65-82-df", 
     "Mod": "1" 
    } 
    }, 
    "Xbars": { 
    "1": { 
     "Module-Type": "Fabric 1", 
     "Ports": "0", 
     "Sw": "NA", 
     "Hw": "1.0", 
     "Model": "ABC", 
     "Status": "ok", 
     "Xbar": "1" 
    } 
    } 
} 
+0

感謝Paul的建議。上面的代碼完美地適用於一個模塊。 然而,對於輸入1.它的拋出鍵錯誤,因爲這裏有一個新的rowname'xbar'。任何想法我們如何處理這一點。 此外,它不是迭代爲輸入2的下一組模塊。 – Aftab

+0

寫完後,我有一種感覺,這將是多個模塊的情況。重寫爲使用itertools.groupby抽出一組行,並在有一組非數據行的情況下進行一些錯誤檢查。你從這裏學不到很多Python,但也許這對你來說是一個有用的代碼示例。 – PaulMcG

1

我有一個解決方案,但它是不漂亮。假設你的整個輸出是在text

import re 
lines = text.split("\n") 
keylines = [line for i, line in enumerate(lines) if len(lines)>(i+1) and "---" in lines[i+1]] 
vallines = [line for i, line in enumerate(lines) if i!=0 and "---" in lines[i-1]] 
keys = re.split(" +", " ".join(keylines)) 
vals = re.split(" +", " ".join(vallines)) 
result = dict(zip(keys, vals)) 

輸出:

{ 
    "Mod": "1", 
    "Ports": "48", 
    "Module-Type": "1/2/4/8 Gbps FC/Supervisor-3", 
    "Model": "DS-C9148-K9-SUP", 
    "Status": "active *", 
    "Sw": "6.2(17)", 
    "Hw": "1.1", 
    "World-Wide-Name(s) (WWN)": "20:01:54:7f:ee:df:88:f8 to 20:30:54:7f:ee:df:88:f8", 
    "MAC-Address(es)": "c0-8c-60-65-82-dc to c0-8c-60-65-82-df", 
    "Serial-Num": "JAF1736ALLM" 
} 

它做以下假設,當他們是不是真的會打破:

  • 沒有值包含連續多個空格。
  • 「字段」之間至少有兩個空格。
  • 在與破折號的行中,至少有一個3破折號的段。