gpt4 book ai didi

python - 从字符串python block 中获取第一个字符串/列

转载 作者:行者123 更新时间:2023-12-01 01:18:16 24 4
gpt4 key购买 nike

VSCODE 上的脚本:

import paramiko
ip =x.x.x.x
port = x
username = username
password = password
cmd='show interface status'

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)

stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print (resp)

当前输出:

PORT          NAME           STATUS       VLAN         DUPLEX  SPEED
Gi1/0/11 notconnect 33 auto auto
Gi1/0/12 notconnect 6 auto auto
Gi1/0/13 notconnect 60 auto auto

期望的输出:

PORT             STATUS       VLAN         
Gi1/0/11 notconnect 33
Gi1/0/12 notconnect 6
Gi1/0/13 notconnect 60

我的计划是在终端输出中提取这些字符串列并将这些字符串传输到 Excel 文件,但目前我在获取这些字符串时遇到问题,我尝试循环仅从一列获取字符串,但是当我我们观察到,列中的空白值在数组中按此格式进行编号(请参见下面的示例),没有任何内容分配给空白值。

PORT         NAME        STATUS        VLAN      DUPLEX     SPEED
string [0] string [?] string [1] string [2] string[3] string [4]

Gi1/0/11 notconnect 33 auto auto
string [5] string[?] string[6] string [7] string[8] string[9]

最佳答案

如果您有所提供的数据:

data = """PORT          NAME           STATUS       VLAN         DUPLEX SPEED
Gi1/0/11 notconnect 33 auto auto
Gi1/0/12 notconnect 6 auto auto
Gi1/0/13 notconnect 60 auto auto
Gi1/0/13 notconnect 60 auto auto
"""

然后首先您可以收集列大小:

headers = {}

header_line = data.split('\n')[0]
name = ''
position = 0
for char in header_line:
if char != ' ' and len(name) != len(name.rstrip()):
headers[name.rstrip()] = position
name = ''
position = 0
name += char
position += 1

headers[name.rstrip()] = None

print(headers)

然后您将获得其大小以字符为单位的列(其中最后一列的长度就是剩下的长度)

{'PORT': 14, 'NAME': 15, 'STATUS': 13, 'VLAN': 13, 'DUPLEX': 7, 'SPEED': None}

当您拥有这样的 map 时,您可以仅提取并打印允许的标题

allowed_headers = ['PORT', 'STATUS', 'VLAN']

for line in data.strip().split('\n'):
for header, length in headers.items():
if length:
value = line[:length]
line = line[length:]
else:
value = line
if header in allowed_headers:
print(value, end="")
print()

-->

PORT          STATUS       VLAN         
Gi1/0/11 notconnect 33
Gi1/0/12 notconnect 6
Gi1/0/13 notconnect 60
Gi1/0/13 notconnect 60

当然,您可以将其收集为字典而不是打印,然后您可以使用该数据执行您想要的操作:

elements = []

for line in data.strip().split('\n')[1:]:
element = {}
for header, length in headers.items():
if length:
value = line[:length]
line = line[length:]
else:
value = line
element[header] = value.rstrip()
elements.append(element)

print(elements)

[{'PORT': 'Gi1/0/11', 'NAME': '', 'STATUS': 'notconnect', 'VLAN': '33', 'DUPLEX': 'auto', 'SPEED': 'auto'}, {'PORT': 'Gi1/0/12', 'NAME': '', 'STATUS': 'notconnect', 'VLAN': '6', 'DUPLEX': 'auto', 'SPEED': 'auto'}, {'PORT': 'Gi1/0/13', 'NAME': '', 'STATUS': 'notconnect', 'VLAN': '60', 'DUPLEX': 'auto', 'SPEED': 'auto'}, {'PORT': 'Gi1/0/13', 'NAME': '', 'STATUS': 'notconnect', 'VLAN': '60', 'DUPLEX': 'auto', 'SPEED': 'auto'}]

注意:如果输出中有制表符,您可能需要扩展脚本:

if char != ' ':

需要更改为:

if char not in ' \t':

关于python - 从字符串python block 中获取第一个字符串/列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54105620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com