pandas 如何将CLI命令输出复制到表

ktca8awb  于 2023-06-04  发布在  其他
关注(0)|答案(1)|浏览(149)

我想将CLI命令输出复制到表中,然后保存为 Dataframe

DD-NN-KK-OOLL(config)#display vlan 0000
 -------------------------------------------------------------------
 Index  Type   State   F S P    VPI  VCI   FLOWTYPE FLOWPARA   LABEL
 -------------------------------------------------------------------
 324  vdl    down    0 3 62   0    35    -        -            390
 452  vdl    down    0 3 62   -    -     -        -            390
 613  vdl    up      0 4 49   0    35    -        -            453
 614  vdl    up      0 4 49   -    -     -        -            453
 617  vdl    down    0 2 26   0    35    -        -            278
 618  vdl    down    0 2 26   -    -     -        -            278
 619  vdl    up      0 2 30   0    35    -        -            282
 620  vdl    up      0 2 30   -    -     -        -            282
 621  vdl    up      0 4 62   0    35    -        -            466
 622  vdl    up      0 4 62   -    -     -        -            466
 627  vdl    down    0 2 20   0    35    -        -            272
 628  vdl    down    0 2 20   -    -     -        -            272

我需要的输出
| 类型|国家|F级|S型|P的|VPI| VCI|标签|
| - -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|
| VDL|向下|0| 3|六十二|0|三十五|三百九十|
| VDL|向下|0| 3|六十二|- 你好|- 你好|三百九十|
| VDL|向上|0| 4|四十九|0|三十五|四百五十三|
| VDL|向上|0| 4|四十九|- 你好|- 你好|四百五十三|
| VDL|向下|0| 2|二十六|0|三十五|二百七十八|
| VDL|向下|0| 3|六十二|- 你好|- 你好|268|

xqk2d5yq

xqk2d5yq1#

您可以用途:

import subprocess
from io import StringIO
​
s = subprocess.check_output(["xclip", "-o"]).decode("UTF-8") #change the command here
​
df = (
    pd.read_csv(StringIO(s), sep="\s\s*", skiprows=2, engine="python").loc[1:]
          #.drop(columns=["Index", "FLOWTYPE, "FLOWPARA"]) # is this optional ? If not, uncomment
)

输出:

print(df)

   Index Type State    F    S     P VPI VCI FLOWTYPE FLOWPARA  LABEL
1    324  vdl  down  0.0  3.0  62.0   0  35        -        -  390.0
2    452  vdl  down  0.0  3.0  62.0   -   -        -        -  390.0
3    613  vdl    up  0.0  4.0  49.0   0  35        -        -  453.0
4    614  vdl    up  0.0  4.0  49.0   -   -        -        -  453.0
5    617  vdl  down  0.0  2.0  26.0   0  35        -        -  278.0
..   ...  ...   ...  ...  ...   ...  ..  ..      ...      ...    ...
8    620  vdl    up  0.0  2.0  30.0   -   -        -        -  282.0
9    621  vdl    up  0.0  4.0  62.0   0  35        -        -  466.0
10   622  vdl    up  0.0  4.0  62.0   -   -        -        -  466.0
11   627  vdl  down  0.0  2.0  20.0   0  35        -        -  272.0
12   628  vdl  down  0.0  2.0  20.0   -   -        -        -  272.0

[12 rows x 11 columns]

相关问题