Reputation: 55
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 = """
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
Upvotes: 2
Views: 41
Reputation: 1508
This is happening because bgp_summary
has some empty lines.
The easiest and most straight forward solution is to convert your bgp_summary
from:
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.
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)
Upvotes: 0
Reputation: 49812
Some of your lines are empty. If you discard those like:
bgp_peers_list_raw = [l.strip() for l in bgp_summary.splitlines()
if l.strip()]
It will work fine.
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)
['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']
Upvotes: 2