Sunday 15 June 2014

stdout - Get list of dicts from another python script? -


I have a program where I need to call a pie script from other pi scripts and related articles Get the list. I have come to know how to call other pi scripts and get the list as a string from the standout, but how do I use it in another script? This is the second script that outputs the second script.

  [[itemkey1 ':' item1 ',' itemkey2 ':' item2 '}, {' itemkey1 ':' item1 ',' itemkey2 ':' item2 '}]   

And I want this list in the first script. I found that a solution is using exec but it raises some security problems and since I want to avoid it.

To get the output from that script in a string, subprocess.check_output And then apply ast.literal_eval to that string to get that object.

  import import subprocessed ret = subprocess.check_output (['python', 'some_script.ip']) dic = ast.literal_eval (ret)   

ast.literal_eval Demo:

  & gt; & Gt; & Gt; Rate = "[['item 1': 'item 1', 'item 2': 'item 2'], {'item 1': 'item 1', 'item 2': 'item2'}] \ N "& gt; & Gt; & Gt; Ast.literal_eval (ret) [{'itemkey2': 'item2', 'itemkey1': 'item1'}, {'itemkey2': 'item2', 'itemkey1': 'item1'}]   ast.literal_eval :  literal_eval (node_or_string)   

securely evaluate an expression node Or a string that contains Python expression. The string or node provided can contain only the following Python literal structures: strings, numbers, topleple, lists, decates, bullion, and none.

No comments:

Post a Comment