I want to create a variable such as 'path' and use it to get the same result when getting data from the dictionary. Is this concatenation possible by doing some type of conversions?
print(dict['detect'][1]['ssh'][0]['Server']) #Result
path = "['ssh'][0]['Server']"
print(dict['detect'][1]{path}) #...should give same result
CodePudding user response:
To answer your immediate question, you can use eval()
to treat the string data as code:
print(eval(f"dict['detect'][1]{path}")
That being said, be very careful when using eval()
, as it introduces major security risks. I don't see a way to use ast.literal_eval()
here, so if at all possible I would suggest avoiding this appoach entirely.