Python 从字符串块中获取第一个字符串/列
VSCODE上的脚本:Python 从字符串块中获取第一个字符串/列,python,excel,Python,Excel,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,st
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]
我将分割输出并仅为输出选择请求的索引:
import re
...
outlines=re.split(r' +', stdout.readlines()) #or re.split(r'\t+',...) for tabs
outlines='\t'.join([outlines[0], outlines[1], outlines[2]])
...
这意味着你的代码:
import paramiko
import re
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=re.split(r' +', stdout.readlines())
resp='\t'.join([outlines[0], outlines[1], outlines[2]])
print (resp)
如果您有提供的数据:
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)
然后,您将有大小为字符的列(其中最后一列的长度正好是剩下的长度)
当您将有这样的地图,然后您可以提取和打印只允许的标题
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()
-->
当然,您可以将其作为dict收集,而不是打印,然后您可以对该数据执行您想要的操作:
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':
if char != ' ':
if char not in ' \t':