Python中的串联顺序错误,并覆盖部分字符串
我从Python脚本中得到一个奇怪的输出 当在涉及Paramiko的实际执行的循环中运行代码时,它似乎颠倒了要打印的变量的顺序,同时覆盖了输出的开始 要处理的所有源文本都是相同的。一个是通过Paramiko通过ssh从路由器抓取的,另一个是我手动抓取并粘贴为局部变量 电流输出:Python中的串联顺序错误,并覆盖部分字符串,python,loops,concatenation,paramiko,overwrite,Python,Loops,Concatenation,Paramiko,Overwrite,我从Python脚本中得到一个奇怪的输出 当在涉及Paramiko的实际执行的循环中运行代码时,它似乎颠倒了要打印的变量的顺序,同时覆盖了输出的开始 要处理的所有源文本都是相同的。一个是通过Paramiko通过ssh从路由器抓取的,另一个是我手动抓取并粘贴为局部变量 电流输出: with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1) Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) wi
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
import paramiko
router_channel.send('sh ip bgp 10.23.24.32\n')
buff = ''
best_test =()
while not buff.endswith('#'):
resp = router_channel.recv(99999)
buff += resp
#DMVPN variable Declaration.
bgp_show = buff
bgp_list = bgp_show.split('Local')
bgp_list.pop(0)
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
best_test = best_list[1]
print '********'
print buff
print '********'
print "Best Path is " + best_test + " with " + "test"
print '********'
bgp_list = bgp.split('Local')
bgp_list.pop(0)
#pprint.pprint( bgp_list )
#print bgp_list[2]
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
print '********'
print bgp
print '********'
print "Best Path is " + best_list[1] + ' with' + local_pref
print '********'
derp@localhost Test Python Scripts]$ python splitty1.py
********
sh ip bgp 10.23.24.32 | exclude local
BGP routing table entry for 10.23.24.32/28, version 122378
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.72 from 10.8.111.72 (10.111.72.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 2
Local
10.8.11.73 from 10.9.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local
10.8.11.73 from 10.8.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 115, valid, internal
rx pathid: 0, tx pathid: 0
********
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
********
[derp@localhost Test Python Scripts]$
所需的输出和带有静态局部变量的输出:
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
import paramiko
router_channel.send('sh ip bgp 10.23.24.32\n')
buff = ''
best_test =()
while not buff.endswith('#'):
resp = router_channel.recv(99999)
buff += resp
#DMVPN variable Declaration.
bgp_show = buff
bgp_list = bgp_show.split('Local')
bgp_list.pop(0)
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
best_test = best_list[1]
print '********'
print buff
print '********'
print "Best Path is " + best_test + " with " + "test"
print '********'
bgp_list = bgp.split('Local')
bgp_list.pop(0)
#pprint.pprint( bgp_list )
#print bgp_list[2]
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
print '********'
print bgp
print '********'
print "Best Path is " + best_list[1] + ' with' + local_pref
print '********'
derp@localhost Test Python Scripts]$ python splitty1.py
********
sh ip bgp 10.23.24.32 | exclude local
BGP routing table entry for 10.23.24.32/28, version 122378
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.72 from 10.8.111.72 (10.111.72.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 2
Local
10.8.11.73 from 10.9.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local
10.8.11.73 from 10.8.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 115, valid, internal
rx pathid: 0, tx pathid: 0
********
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
********
[derp@localhost Test Python Scripts]$
我根本不知道为什么会这样。有关更多信息和执行,请参见下文帮助?
代码:
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
import paramiko
router_channel.send('sh ip bgp 10.23.24.32\n')
buff = ''
best_test =()
while not buff.endswith('#'):
resp = router_channel.recv(99999)
buff += resp
#DMVPN variable Declaration.
bgp_show = buff
bgp_list = bgp_show.split('Local')
bgp_list.pop(0)
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
best_test = best_list[1]
print '********'
print buff
print '********'
print "Best Path is " + best_test + " with " + "test"
print '********'
bgp_list = bgp.split('Local')
bgp_list.pop(0)
#pprint.pprint( bgp_list )
#print bgp_list[2]
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
print '********'
print bgp
print '********'
print "Best Path is " + best_list[1] + ' with' + local_pref
print '********'
derp@localhost Test Python Scripts]$ python splitty1.py
********
sh ip bgp 10.23.24.32 | exclude local
BGP routing table entry for 10.23.24.32/28, version 122378
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.72 from 10.8.111.72 (10.111.72.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 2
Local
10.8.11.73 from 10.9.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local
10.8.11.73 from 10.8.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 115, valid, internal
rx pathid: 0, tx pathid: 0
********
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
********
[derp@localhost Test Python Scripts]$
代码的执行:
dirp@localhost Test Python Scripts]$ python splice.py
********
sh ip bgp 10.23.24.32
BGP routing table entry for 10.23.24.32/28, version 367886
Paths: (6 available, best #5, table default)
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 3
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
router#
********
With testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
********
静态代码:
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
import paramiko
router_channel.send('sh ip bgp 10.23.24.32\n')
buff = ''
best_test =()
while not buff.endswith('#'):
resp = router_channel.recv(99999)
buff += resp
#DMVPN variable Declaration.
bgp_show = buff
bgp_list = bgp_show.split('Local')
bgp_list.pop(0)
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
best_test = best_list[1]
print '********'
print buff
print '********'
print "Best Path is " + best_test + " with " + "test"
print '********'
bgp_list = bgp.split('Local')
bgp_list.pop(0)
#pprint.pprint( bgp_list )
#print bgp_list[2]
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
print '********'
print bgp
print '********'
print "Best Path is " + best_list[1] + ' with' + local_pref
print '********'
derp@localhost Test Python Scripts]$ python splitty1.py
********
sh ip bgp 10.23.24.32 | exclude local
BGP routing table entry for 10.23.24.32/28, version 122378
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.72 from 10.8.111.72 (10.111.72.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 2
Local
10.8.11.73 from 10.9.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local
10.8.11.73 from 10.8.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 115, valid, internal
rx pathid: 0, tx pathid: 0
********
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
********
[derp@localhost Test Python Scripts]$
在不将其放入循环但在本地分配变量时输出:
with testis 10.8.11.72 from 10.8.11.72 (10.111.72.1)
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
import paramiko
router_channel.send('sh ip bgp 10.23.24.32\n')
buff = ''
best_test =()
while not buff.endswith('#'):
resp = router_channel.recv(99999)
buff += resp
#DMVPN variable Declaration.
bgp_show = buff
bgp_list = bgp_show.split('Local')
bgp_list.pop(0)
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
best_test = best_list[1]
print '********'
print buff
print '********'
print "Best Path is " + best_test + " with " + "test"
print '********'
bgp_list = bgp.split('Local')
bgp_list.pop(0)
#pprint.pprint( bgp_list )
#print bgp_list[2]
for x in bgp_list:
if 'best' in x:
best_list = x.split('\n')
for x in best_list:
best_list = [x.strip(' ') for x in best_list]
local_pref = best_list[2]
local_pref = local_pref.split(',')
local_pref = local_pref[2]
print '********'
print bgp
print '********'
print "Best Path is " + best_list[1] + ' with' + local_pref
print '********'
derp@localhost Test Python Scripts]$ python splitty1.py
********
sh ip bgp 10.23.24.32 | exclude local
BGP routing table entry for 10.23.24.32/28, version 122378
Advertised to update-groups:
1 2 3 4 5 6 7
9
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.111.72 from 10.8.111.72 (10.111.72.2)
Origin IGP, metric 0, localpref 105, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 1
Local, (Received from a RR-client)
10.8.11.72 from 10.8.11.72 (10.111.72.1)
Origin IGP, metric 0, localpref 110, valid, internal
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.11.73 from 10.8.11.73 (10.111.73.1)
Origin IGP, metric 0, localpref 120, valid, internal, best
rx pathid: 0, tx pathid: 0x0
Refresh Epoch 2
Local
10.8.11.73 from 10.9.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local
10.8.11.73 from 10.8.0.1 (10.15.1.41)
Origin IGP, metric 0, localpref 120, valid, internal
Originator: 10.111.73.1, Cluster list: 10.15.1.41
rx pathid: 0, tx pathid: 0
Refresh Epoch 2
Local, (Received from a RR-client)
10.8.111.73 from 10.8.111.73 (10.111.73.2)
Origin IGP, metric 0, localpref 115, valid, internal
rx pathid: 0, tx pathid: 0
********
Best Path is 10.8.11.73 from 10.8.11.73 (10.111.73.1) with localpref 120
********
[derp@localhost Test Python Scripts]$
这些台词使我感到不安-- 显然,您正在通过best_列表中的值在循环中修改best_列表的值。即使你认为你知道那是什么,我也不知道
尝试为“内部”最佳\u列表使用不同的变量名,看看是否可以解决任何问题。
router\u channel.recv
返回Windows格式文本,其中每行以'\r\n'
结尾。但是您创建了一个带有x.split('\n')
的列表。这意味着每一行(可能最后一行除外)都将以'\r'
结尾
在非Windows系统上,如果打印出'\r'
,则会将光标移回第1列,而不推进该行。因此,当您这样做时:
print "Best Path is " + best_test + " with " + "test"
您正在打印:
Best Path is blah blah blah\r with test
结果如下:
with testis blah blah blah
最简单的解决方案是使用该方法而不是
str.split('\n')
。自动考虑Windows线条端点的:
返回字符串中的行列表,在行边界处断开。此方法使用分割线的方法
如果你遵循链接,它会解释通用换行符:
一种解释文本流的方式,其中以下所有内容都被识别为行尾:Unix行尾约定'\n'
、Windows约定'\r\n'
、旧的Macintosh约定'\r'
输出转储显示带有testis的
(但前面有空格),顶部的摘要显示带有testis的(小写,无空格)。是哪一个?很难想象Python如何在代码中没有大写字母时大写字母w
,或者如何神奇地删除单词前的空格,因此,这两个输出都不太可能…修复了,因为在我尝试将两个脚本保持在同一个位置时,这是一个格式错误,直到关键点。您修复了什么?我没有看到任何编辑历史记录。“当前输出”和“代码执行”部分仍然显示不同的内容,它们都与您发布的代码可能打印的内容不匹配。我尝试使用在最佳列表中为x使用:最佳项目=[x.strip(“”)在最佳列表中为x使用]
并且输出没有发生任何变化。先生,您是我的英雄。工作得很有魅力。将best\u list=x.splitlines('\n')
更改为best\u list=x.splitlines()
。