Python Forum
Output variables from a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Output variables from a list (/thread-12168.html)



Output variables from a list - MTom5 - Aug-12-2018

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)



RE: Output variables from a list - scidam - Aug-12-2018

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



RE: Output variables from a list - perfringo - Aug-13-2018

One possible way to look at this problem:

Why you don't want to dynamically create variables