#1
|
|||
|
|||
Reference a new dictionary key with a variable
I want to read 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. Quote:
|
#2
|
|||
|
|||
Hi slouw,
This seems more like a Python question than a SecureCRT question: https://docs.python.org/2.7/library/...ing-types-dict (I personally think there are better resources than the above "official" documentation, but you would need to search those out yourself.) But what do you mean by "This does not work"? Quote:
You need to add the key+item into the dictionary as a pair and I am not sure any of your code is doing that. Instead it looks like you are replacing key with item.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
Thanks for reply and link.
By "this does not work" I mean that I do not end up with a dictionary populated as shown earlier in the code fragment. This is what I want my dictionary to look like once I have read in the parameters from my text file: Code:
Dict_Param = { "<IP_ADDRESS>": "10.10.10.10", "<PREFIXLENGTH>": "24", "<HOSTNAME>": "blahblah" } |
#4
|
|||
|
|||
Hi slouw,
Thanks for the clarification. An eagle-eyed colleague spotted what may be the issue. You are using ticks around the variable name for assignment, making it a literal instead of a variable. ![]() Dict_Param['KeyStr[0]'] = AssignStr[0] should be Dict_Param[KeyStr[0]] = AssignStr[0] If you fix it, the issue should be resolved.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#5
|
|||
|
|||
Working sample
And so it does thankyou.
Your help much appreciated... This is a complete working code fragment Code:
# $language = "python" # $interface = "1.0" # Dict_Param = {} KeyStr = ["<IP_ADDRESS>","<PREFIXLENGTH>","<HOSTNAME>"] AssignStr = ["10.10.10.10","24","blahblah"] print Dict_Param for xxx in range(0, 3): Dict_Param[KeyStr[xxx]] = AssignStr[xxx] print Dict_Param |
#6
|
|||
|
|||
The canonical way would probably be something like:
Code:
# $language = "python" # $interface = "1.0" # KeyStr = ["<IP_ADDRESS>","<PREFIXLENGTH>","<HOSTNAME>"] AssignStr = ["10.10.10.10","24","blahblah"] Dict_Param = dict(zip(KeyStr, ValueStr)) |
#7
|
|||
|
|||
Thanks Ogun.. interesting read and helpful
|
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|