Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-05-2019, 02:38 AM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 143
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:
# $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
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Reply With Quote
  #2  
Old 05-06-2019, 10:47 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,945
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:
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# This does not work
What error do you get? What results do you get? How does that differ from the expected results?

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
Reply With Quote
  #3  
Old 05-06-2019, 01:21 PM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 143
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"
}
I read the data from a file into a list easy enough, but assigning the list entries to key entries is where I get stuck.
Reply With Quote
  #4  
Old 05-06-2019, 01:29 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,945
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
Reply With Quote
  #5  
Old 05-06-2019, 02:38 PM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 143
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
Reply With Quote
  #6  
Old 05-07-2019, 02:53 AM
ogun ogun is offline
Registered User
 
Join Date: Sep 2013
Posts: 15
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))
Though it looks like you are parsing a csv file, in which case I would probably look into the csv module available in the standard library, more specifically the DictReader class. See here for details: https://docs.python.org/2.7/library/...csv.DictReader
Reply With Quote
  #7  
Old 05-07-2019, 03:54 AM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 143
Thanks Ogun.. interesting read and helpful
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 05:19 AM.