Adam Freemotion
Adam Freemotion

Reputation: 43

Creating a list from a for loop to use in another for loop in Python

I used Netmiko to connect to a device and ran 2 commands and saved them into 2 separate text files. Content of first file look something like this:

Physical interface: xe-0/0/0
    Laser output power                        :  0.5290 mW / -2.77 dBm
    Laser receiver power                      :  0.4988 mW / -3.02 dBm
Physical interface: xe-0/0/1
    Laser output power                        :  0.5390 mW / -2.68 dBm
    Laser receiver power                      :  0.5807 mW / -2.36 dBm
Physical interface: xe-1/0/1
    Laser output power                        :  0.6020 mW / -2.20 dBm
    Laser receiver power                      :  0.5307 mW / -2.75 dBm
Physical interface: xe-1/0/2
    Laser output power                        :  0.7740 mW / -1.11 dBm
    Laser receiver power                      :  0.0000 mW / - Inf dBm
Physical interface: xe-1/0/3
    Laser output power                        :  0.5550 mW / -2.56 dBm
    Laser receiver power                      :  0.5472 mW / -2.62 dBm
Physical interface: xe-1/0/5
    Laser output power                        :  0.6280 mW / -2.02 dBm
    Laser receiver power                      :  0.4580 mW / -3.39 dBm

and content of 2nd text file:

Interface       Admin Link Description
xe-1/0/1        up    up   test-desc-101
xe-1/0/3        up    up   CAN-1-OLT
xe-1/0/5        up    up   test-desc-105
xe-1/0/5.100    up    up   CANALU-UPLINK
xe-1/0/6        up    down test-desc-106
ae40            up    up   CAN-1-OLT-LAG
ae40.19         up    up   CAN-1-OLT-MGMT

I opened them with "with open()" and iterate every line just like the following:

with open("toutput_des.txt", "r") as meal_no_empty:
  for line in meal_no_empty:
    stripped_ifdes = line.strip()
    splitted_ifdes = stripped_ifdes.split(" ")

and for the second file:

with open("toutput_sfp.txt", "r") as file2:
  print("Interface ", " Optic out pw ", " optic rec pw")
  for line in file2:
    x = 0
    if "xe-" in line:
      stripped_ifname = line.strip()
      splitted_ifname = stripped_ifname.split(" ")
      x = 1
          
    elif "output" in line:
      stripped_output_power = line.strip()
      splitted_output_power = stripped_output_power.split (" ")
      x = 1
    elif "receiver" in line:
      stripped_rec_power = line.strip()
      splitted_rec_power = stripped_rec_power.split (" ")
      x = 0
    else:
      pass
    if x == 0:
      print(splitted_ifname,"  ",splitted_output_power,"        ",splitted_rec_power)

My goal is capturing description of the interfaces in one file and add them to the second file. I thought if I could make a global list from values generated in each for loop iteration, then I could use call members of that global list in the second for loop.

I'm a beginner in programming/scripting world, so maybe I'm not able to ask my question very clearly or in a right way. So as a simple summary, I can ask, how to create a list and populate the items of the list based on the data generated via for loop iteration and then use the list inside another for loop?

Thanks.

Upvotes: 0

Views: 147

Answers (1)

Mert Kulac
Mert Kulac

Reputation: 294

There is a way to use data in two for loops within each other. First you need to create two def functions. First you have to create two def functions and make your list usable in a different function with "global x(whatever)" inside each function.

  1. The output list of the for loop is "vlan_list" as in the example below.

First def example:

def switch_connection(device_ip):

    global vlan_list # !! this is the list name you want to exclude from the function 

    vlan_list = [] # I created a list

    for line_fnk2 in output_list_fnk2:
        if int(port_vlan2) > 200 and int(port_vlan2) < 4096:
            vlan_list.append(port_vlan2) 

Second def example:

def connectionbekci(ip):

    command.send("display int des | i {}".format(vlan))
    command.expect(UPE_prompt)
    routing = command.current_output_clean
    routing = routing.splitlines()
    result = [x for x in routing if "up" and "100" in x]
    result2 = result[0]
    result3 = ''.join(result2)

Here we use the variables or lists in the functions, one inside the other. In order to use vlan_list in my second function, I made a new for under the for and defined it as "vlan".

for host in hosts:
    switch_connection(host)
    for vlan in vlan_list: # 
        description(connectionbekci(host),vlan) 

Upvotes: 0

Related Questions