Python Forum
Output variables from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output variables from a list
#1
I have the following code that uses Pandas and filters an excel spreadsheet containing IP Addresses.
The code filters the spreadsheet and narrows it down to 2 rows and then prints the result.
How do i get the contents of this list separate and not including the index so i can use them as variables
i.e as:
IP_Add_1 = 10.123.4.5
IP_Add_2 = 10.123.4.6

    #Filter rows by column E (Station name) for results with cell = Asset No. (i.e "G04")
    xlsx_filter1 = IP_Plan.index[IP_Plan['Station name'] == IPP2.get()].tolist()

    IP_Plan = IP_Plan.loc[xlsx_filter1,:]

    #Filter rows by column C (Type) for results with cell = Device Type (i.e "IP Telephone - Norphonic N-K1")
    xlsx_filter2 = IP_Plan.index[IP_Plan['Type'] == "IP Telephone - Norphonic N-K1"].tolist()

    IP_Plan = IP_Plan.loc[xlsx_filter2,:]

    #File cell value by column B (IP address)
    Output_IP_Address = IP_Plan["IP address"]

    print(Output_IP_Address)
Reply
#2
Assume you have a list of ip addresses:
ip_addr_list = ['123.123.123.123', '111.111.111.111', '8.8.8.8']
If you want to create a set of locally visible variables, e.g. IP_addr_1='123.123.123.123', IP_addr_2='111.111.111.111' etc, try to use locals():

for ind, item in enumerate(ip_addr_list):
    locals()['IP_addr_%s' % (ind + 1)] = item
Reply
#3
One possible way to look at this problem:

Why you don't want to dynamically create variables
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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