I am trying to get the fist item in each line within the bgp_summary string and create a new list of these. I am using below code for this purpose, but getting "list index out of range" error. I believe that the list index I am referring is not out of range but not sure why this error is happening. Would appreciate if some one can help.
我正在尝试在bgp_summary字符串内的每一行中获取第一个项目,并创建一个新的列表。我正在使用下面的代码来实现这个目的,但是将“列表索引超出范围”错误。我认为我所指的列表索引并没有超出范围,但不确定为什么会出现这个错误。如果有人能帮忙,我会很感激。
bgp_summary = """
10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ
"""
bgp_peers_list_raw = []
bgp_peers_list_refined = []
bgp_peers_list_raw = bgp_summary.splitlines()
n=0
for n in range (len(bgp_peers_list_raw)):
list_raw1 = []
list_raw1 = bgp_peers_list_raw[n].split()
bgp_peers_list_refined.append(list_raw1[0])
bgp_peers_list_refined.append(list_raw1[0])
IndexError: list index out of range
2 个解决方案
#1
2
Some of your lines are empty. If you discard those like:
你的一些台词是空的。如果你丢弃那些东西:
Code:
bgp_peers_list_raw = [l.strip() for l in bgp_summary.splitlines()
if l.strip()]
It will work fine.
它会正常工作。
Test Code:
bgp_summary = """
10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ
"""
bgp_peers_list_raw = []
bgp_peers_list_refined = []
bgp_peers_list_raw = [l.strip() for l in bgp_summary.splitlines()
if l.strip()]
n = 0
for n in range(len(bgp_peers_list_raw)):
list_raw1 = []
list_raw1 = bgp_peers_list_raw[n].split()
bgp_peers_list_refined.append(list_raw1[0])
bgp_peers_list_refined.append(list_raw1[0])
print(bgp_peers_list_refined)
Results:
['10.0.13.213', '172.46.42.134', '172.57.13.1', '172.57.13.249',
'172.57.13.250', '172.57.13.252', '172.57.13.252']
#2
0
This is happening because bgp_summary
has some empty lines.
这是因为bgp_summary有一些空行。
The easiest and most straight forward solution is to convert your bgp_summary
from:
最简单和最直接的解决方案是将bgp_summary从:
bgp_summary = """
10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ
"""
to:
:
bgp_summary = """10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ"""
which while not as nice looking fixes your problem.
这虽然不是很好的解决问题的方法。
A more complicated solution that leaves your bgp_summary
untouched would be a small modification to your for loop.
一个更复杂的解决方案,将您的bgp_summary保持不变,这将是对您的for循环的一个小修改。
Instead of checking every line like this:
而不是像这样检查每一行:
range(len(bgp_peers_list_raw))
You could exclude the empty ones at the start and at the end like this:
你可以在开始的时候把空的排除在外,最后就像这样:
#2 empty lines at the start
#1 empty line at the end
range(2, len(bgp_peers_list_raw)-1)
#1
2
Some of your lines are empty. If you discard those like:
你的一些台词是空的。如果你丢弃那些东西:
Code:
bgp_peers_list_raw = [l.strip() for l in bgp_summary.splitlines()
if l.strip()]
It will work fine.
它会正常工作。
Test Code:
bgp_summary = """
10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ
"""
bgp_peers_list_raw = []
bgp_peers_list_refined = []
bgp_peers_list_raw = [l.strip() for l in bgp_summary.splitlines()
if l.strip()]
n = 0
for n in range(len(bgp_peers_list_raw)):
list_raw1 = []
list_raw1 = bgp_peers_list_raw[n].split()
bgp_peers_list_refined.append(list_raw1[0])
bgp_peers_list_refined.append(list_raw1[0])
print(bgp_peers_list_refined)
Results:
['10.0.13.213', '172.46.42.134', '172.57.13.1', '172.57.13.249',
'172.57.13.250', '172.57.13.252', '172.57.13.252']
#2
0
This is happening because bgp_summary
has some empty lines.
这是因为bgp_summary有一些空行。
The easiest and most straight forward solution is to convert your bgp_summary
from:
最简单和最直接的解决方案是将bgp_summary从:
bgp_summary = """
10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ
"""
to:
:
bgp_summary = """10.0.13.213 65510 658 86 0 0 2d 3:43:06 Establ
172.46.42.134 65513 819 7 0 11 2d 3:50:49 Establ
172.57.13.1 65501 15427 52 0 0 0d 2:26:01 Establ
172.57.13.249 65513 13449 2517 0 0 2d 3:50:21 Establ
172.57.13.250 65513 134 2515 0 0 4d 3:50:21 Establ
172.57.13.252 65513 46 142 0 0 3:50:32 Establ"""
which while not as nice looking fixes your problem.
这虽然不是很好的解决问题的方法。
A more complicated solution that leaves your bgp_summary
untouched would be a small modification to your for loop.
一个更复杂的解决方案,将您的bgp_summary保持不变,这将是对您的for循环的一个小修改。
Instead of checking every line like this:
而不是像这样检查每一行:
range(len(bgp_peers_list_raw))
You could exclude the empty ones at the start and at the end like this:
你可以在开始的时候把空的排除在外,最后就像这样:
#2 empty lines at the start
#1 empty line at the end
range(2, len(bgp_peers_list_raw)-1)