Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list iterate .. help
#1
Below script read line from ARP file and split line into multiple list, like IP, MAC, interface and numeric interface,

arp file details are as below

Quote:10.10.1.2 21050 0030.6e4a.362a GigabitEthernet1/0/2.1820150
10.10.1.3 21590 f01f.aff1.a65e GigabitEthernet1/0/2.1820150
10.11.1.1 4640 0019.560b.9241 GigabitEthernet1/0/2.115
10.11.1.2 21340 0017.e029.2c41 GigabitEthernet1/0/2.125
10.11.1.3 7980 001a.a226.70c1 GigabitEthernet1/0/2.135
10.11.1.4 16410 001a.e36f.f6c1 GigabitEthernet1/0/2.145
10.11.1.5 6730 001a.a226.8fc1 GigabitEthernet1/0/2.155
10.11.1.6 8240 001a.e36f.07c1 GigabitEthernet1/0/2.165


IP = 10.10.1.2, mac = 001a.e36f.07c1, interface = GigabitEthernet1/0/2.165, numeric_interface = 1/0/2.165

matching numeric_interface and printing svlan and cvlan in line.

sample conf is
interface gigabitEthernet 3/0/6.9332109
svlan id 933 2109
ip description fup16-16384_4-0220365059
ip unnumbered loopback 123252130
no ip proxy-arp
no ip redirects
!
interface gigabitEthernet 3/0/6.9332537
svlan id 933 2537
ip description res16-2048-0221468429
ip unnumbered loopback 240
no ip proxy-arp
no ip redirects
!
interface gigabitEthernet 3/0/6.9332573
!
interface gigabitEthernet 3/0/6.9332654
svlan id 933 2654
ip description fup16-10240-0221460185
ip unnumbered loopback 229
no ip proxy-arp
no ip redirects

=-=-=-=-=-=-=-=-=-

svlan id 933 2654

in below above line:- 993 is svlan and 2654 cvlan

import re
#lists to store details
ip = []
mac = []
interface = []
numric_interface =[]
#with open('E320conf.txt','r') as conffile:
#     content = conffile.readlines()
with open('E320arp.txt','r') as arpfile:
        for line in arpfile:
            #arp_line = line.strip()
            arp = re.sub('\s{2,}',' ',line).split(' ')
            # above line to remove multiple spaces and replace with single space
            ip.append(arp[1])
            #append ip address in list
            mac.append(arp[3])
            #append mac address in list
            interface.append(arp[4])
            #append interface in list
            numric_interface.append(arp[4][15:])
            #slice and append Numric interface
with open('E320conf.txt','r') as conf_file:
     content = conf_file.readlines()
     data = iter(content)
     for conf_file_line in data:
         for x in numric_interface,ip, mac:
             if 'svlan' in conf_file_line:
                 svlan = conf_file_line.split(' ')[3]
                 cvlan = conf_file_line.split(' ')[4]
                 print('{} {} {} {} {}'.format(ip[1],mac[1],numric_interface[1],svlan,cvlan))
Output:
10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 949 2503 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025
how to iterate list and print on same line
Reply
#2
The print function has an 'end' parameter which has a default value '\n'. You can change it with "" or " " if you want space separator.
>>> for x in range(5):
...     print(x, end=' ')
... 
0 1 2 3 4
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Hi Wavic,

tried as below, but still printing on same line.
print('{} {} {} {} {}'.format(ip[1],mac[1],numric_interface[1],svlan,cvlan, end=' '))
Output:
10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025 10.10.1.3 f01f.aff1.a65e 1/0/2.1820150 971 3025
Reply
#4
How many times you will ask virtually the same question and will learn that line from file ends with new line \n and you need to strip it? Moreover your code has it and it is commented out...
Reply
#5
(Mar-19-2018, 10:30 AM)buran Wrote: How many times you will ask virtually the same question and will learn that line from file ends with new line \n and you need to strip it? Moreover your code has it and it is commented out...

Yes.. same is was there in my last script... you caught me.. again Clap
Reply
#6
Thanks Buran,


import re
ip = []
mac = []
interface = []
numric_interface =[]
#with open('E320conf.txt','r') as conffile:
#     content = conffile.readlines()
with open('E320arp.txt','r') as arpfile:
        for line in arpfile:
            arp_line = line.strip('\n')
            arp = re.sub('\s{2,}',' ',arp_line).split(' ')
            # above line to remove multiple spaces and replace with single space
            ip.append(arp[1])
            #append ip address in list
            mac.append(arp[3])
            #append mac address in list
            interface.append(arp[4])
            #append interface in list
            numric_interface.append(arp[4][15:])
            #slice and append Numric interface
with open('E320conf.txt','r') as conf_file:
     content = conf_file.readlines()
     data = iter(content)
     for conf_file_line in data:
         for x in numric_interface:
             if 'svlan' in conf_file_line:
                 vlans = conf_file_line.strip('\n')
                 scvlan = re.sub('\s{2,}',' ',vlans)
                 #re.sub('\s{2,}',' ',
                 #svlan = conf_file_line.split(' ')[3]
                 #cvlan = conf_file_line.split(' ')[4]
                 svlan = scvlan.split(' ')[3]
                 cvlan = scvlan.split(' ')[4]
                 print('{} {} {} {} {} {}'.format(ip[1],mac[1],interface[1],numric_interface[1],svlan,cvlan, end=' '))
Output:
10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 933 2654 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 933 2654 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 933 2654 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 933 2654 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 933 2654 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 949 2503 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 949 2503 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 949 2503 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 949 2503 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 949 2503 10.10.1.3 f01f.aff1.a65e GigabitEthernet1/0/2.1820150 1/0/2.1820150 949 2503
how to iterate lists, i am printing first position only
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to iterate through a list... t4keheart 2 1,673 Feb-07-2020, 08:15 PM
Last Post: t4keheart
  list of lists iterate only sends the last value batchenr 1 1,898 Sep-24-2019, 07:04 AM
Last Post: Gribouillis
  Iterate through a list of dictionary and append a new value. erina 1 2,099 May-16-2019, 09:55 AM
Last Post: perfringo
  iterate through a list with comparison of expression Alexandro 6 3,415 Jan-31-2019, 05:16 PM
Last Post: Scorpio
  How to iterate through a list and search for a value fad3r 2 2,851 Jan-23-2018, 02:07 AM
Last Post: fad3r
  having dictionary and list to iterate in for loop Annie 5 5,374 Jan-05-2017, 01:05 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020