英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论