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

huangapple go评论67阅读模式
英文:

How to copy CLI command output to table

问题

我想将CLI命令输出复制到表格中,然后保存为数据框架。

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
----------------------------------------------------------------------

我需要的输出:

| Type | State | F | S | P | VPI | VCI | LABEL |
|:----:|:-----:|:-:|:-:|:-:|:---:|:---:|:-----:|
| vdl  | down  | 0 | 3 | 62|  0  |  35 | 390  |
| vdl  | down  | 0 | 3 | 62|  -  |  -  | 390  |
| vdl  | up    | 0 | 4 | 49|  0  |  35 | 453  |
| vdl  | up    | 0 | 4 | 49|  -  |  -  | 453  |
| vdl  | down  | 0 | 2 | 26|  0  |  35 | 278  |
| vdl  | down  | 0 | 2 | 26|  -  |  -  | 268  |
英文:

I want to copy CLI command output to table then save as data frame

 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

The output I need

Type State F S P VPI VCI LABEL
vdl down 0 3 62 0 35 390
vdl down 0 3 62 - - 390
vdl up 0 4 49 0 35 453
vdl up 0 4 49 - - 453
vdl down 0 2 26 0 35 278
vdl down 0 3 62 - - 268

答案1

得分: 1

你可以使用以下代码:

import subprocess
from io import StringIO

s = subprocess.check_output(["xclip", "-o"]).decode("UTF-8") # 在这里更改命令

df = (
    pd.read_csv(StringIO(s), sep="\s\s*", skiprows=2, engine="python").loc[1:]
          #.drop(columns=["Index", "FLOWTYPE, "FLOWPARA"]) # 这部分是可选的吗?如果不是,取消注释
)

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]
英文:

You can use :

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
)


Output :

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]

huangapple
  • 本文由 发表于 2023年5月29日 19:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定