gpt4 book ai didi

python - 在 Python 中以错误的顺序连接并覆盖字符串的一部分

转载 作者:行者123 更新时间:2023-11-28 22:47:35 25 4
gpt4 key购买 nike

我从 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) 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 '********'

代码的执行:

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

静态代码:

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

最佳答案

router_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.splitlines()方法而不是 str.split('\n')。这会自动考虑 Windows 行结尾:

Return a list of the lines in the string, breaking at line boundaries. This method uses the universal newlines approach to splitting lines…

如果您点击链接,它会解释通用换行符:

A manner of interpreting text streams in which all of the following are recognized as ending a line: the Unix end-of-line convention '\n', the Windows convention '\r\n', and the old Macintosh convention '\r'

关于python - 在 Python 中以错误的顺序连接并覆盖字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26109095/

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