Python Forum

Full Version: Output variables from a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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
One possible way to look at this problem:

Why you don't want to dynamically create variables