Reference new dictionary keys with a variable - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Reference new dictionary keys with a variable (/thread-18128.html) |
Reference new dictionary keys with a variable - slouw - May-06-2019 I want to read text files with parameters and assignments in order to configure a new device. It seemed correct to read variables and their assignments into a dictionary from a text file. But I have hit a snag in that I cannot work out how to read variables directly into a dictionary. I have stripped down my script to the following to try and illustrate this. Grateful for any replies.. # $language = "python" # $interface = "1.0" # import time import datetime import sys #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # This is how I want me dictionary to look after # assignment from reading a text file #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Dict_Param = { "<IP_ADDRESS>": "10.10.10.10", "<PREFIXLENGTH>": "24", "<HOSTNAME>": "blahblah" } Dict_Param = {} #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # Variables and their assignments are read from # a text file into 2 lists as shown # I have this working just stripped out for simplicity #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyStr = ["<IP_ADDRESS>","<PREFIXLENGTH>","<HOSTNAME>"] AssignStr = ["10.10.10.10", "24", "blahblah"] #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # This does not work # But I hope it illustrates what I am trying # and failing to do # That is add the pairs defined in the 2 lists above # as key entries in my dictionary #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Dict_Param['KeyStr[0]'] = AssignStr[0] Dict_Param['KeyStr[1]'] = AssignStr[1] Dict_Param['KeyStr[2]'] = AssignStr[2] print Dict_Param #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RE: Reference new dictionary keys with a variable - nilamo - May-06-2019 (May-06-2019, 07:17 PM)slouw Wrote:Dict_Param['KeyStr[0]'] = AssignStr[0] Dict_Param['KeyStr[1]'] = AssignStr[1] Dict_Param['KeyStr[2]'] = AssignStr[2] Remove the quotes, and that would work as you expect. Or, you can do it the easy way and use a loop: for index in range(len(KeyStr)): if index < len(AssignStr): key = KeyStr[index] val = AssignStr[index] Dict_Param[key] = val RE: Reference new dictionary keys with a variable - buran - May-06-2019 keys = ["<IP_ADDRESS>","<PREFIXLENGTH>","<HOSTNAME>"] values = ["10.10.10.10", "24", "blahblah"] dict_params = dict(zip(keys, values))but it's interesting how you end up with two lists by reading from a text file - you can read from file line by line and directly assign value to key in the dict. I don't give example, because it's not clear how the file looks like RE: Reference new dictionary keys with a variable - Yoriz - May-06-2019 You can use zip to pair up the key and value in a for loop dict_param = {} keystr = ["<IP_ADDRESS>","<PREFIXLENGTH>","<HOSTNAME>"] assignstr = ["10.10.10.10", "24", "blahblah"] for key, value in zip(keystr, assignstr): dict_param[key] = value print(dict_param)
RE: Reference new dictionary keys with a variable - slouw - May-07-2019 OMG what wonderful answers! Thank you all!!! Stupendous! Amazing! Thank you all!!! |