I'm very new to Python and am working to redo my groovy/PowerShell code in Python. I've hit a snag when trying to import from another python file.
I have a list of dictionaries in a second python file that I call with the code below
from testdict import Dev1
in this case I am only importing a single Dictionary called Dev1, but I have 15 dictionaries in that file. I need to be able to variablize that and I think I can do it one of 2 ways.
either import the entire file (would rather not but I can) and then only select the dictionary I need. However I hit a snag when I try to reference the specific dictionary.
import testdict
dictionary = testdict.env -- angry
Or I can only import the single dictionary, but I hit a snag in the import statement
from testdict import env -- angry
Again, the code works without a variable for the dictionary, but I need to variablize it so my code works in more than one situation.
Below is the code I currently have, and all I'm looking to do is replace Dev1 with a variable I can pass into the method.
The script is doing a find and replace on T-SQL files based on the dictionary selected.
Any help would be appreciated!
def main(inputfile, outputfile, env):
from testdict import Dev1
#import testdict
print (env)
with open(inputfile, 'r') as file :
filedata = file.read()
dictionary = Dev1
for key in dictionary.keys():
filedata = filedata.replace(key, dictionary[key])
# Write the file out again
with open(outputfile, 'w') as file:
file.write(filedata)
main('DropandAddDBUsers.sql','DropandAddDBUsers2.sql', 'Dev1')
testdictnamedenvor not? Ifenvis a variable that contains the name of the variable, you can usegetattr(testdict, env), but you should probably have a single dict of dicts intestdictinstead.