The following code needs to get the value of the element, but the output is empty

[reflection.assembly]::LoadFrom("C:\Program Files\IronPython 2.7\IronPython.dll")
$py = [ironpython.hosting.python]::CreateEngine()
$pyv = $py.CreateScope()
$pyc = $py.CreateScriptSourceFromString("d = {'one':1,'two':2}")
$pyc.Execute($pyv)
$d = $pyv.GetVariable("d")
$d.one
$d.two

1 Answers

0
Abhijith pk On Best Solutions

You can use the get("key") function or $variable["key"] to get dictionary values :

$engine = [ironpython.hosting.python]::CreateEngine()
$pyv = $engine.CreateScope()
$pyc = $engine.CreateScriptSourceFromString("d = {'one':1,'two':2}")
$pyc.Execute($pyv)
$d = $pyv.GetVariable("d")
$d.get("one")
$d["two"]

This is the expected behavior with python dictionaries,if you try get-member on the object then you can see that the properties one and two are not there but powershell won't throw error even if you try to access properties that are not there