質問

I have a program where I need to call another py script from one py script and get a list of dicts from it. I've figured out how to call the other py script and get list as a string from stdout but how do I use it in the second script ? Here is what the second script outputs.

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

And I need this list in the first script. One solution I found is using exec but that brings up some security issues and since I would like to avoid it.

役に立ちましたか?

解決

Use subprocess.check_output to get the output from that script in a string and then apply ast.literal_eval to that string to get the dict object.

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

ast.literal_eval demo:

>>> ret = "[{'itemkey1': 'item1', 'itemkey2': 'item2'}, {'itemkey1': 'item1', 'itemkey2': 'item2'}]\n"
>>> ast.literal_eval(ret)
[{'itemkey2': 'item2', 'itemkey1': 'item1'}, {'itemkey2': 'item2', 'itemkey1': 'item1'}]

help on ast.literal_eval: literal_eval(node_or_string)

Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

他のヒント

ast.literal_eval is completely safe

>>> import ast
>>> output = "[{'itemkey1': 'item1', 'itemkey2': 'item2'}, {'itemkey1': 'item1', 'itemkey2': 'item2'}]"
>>> ast.literal_eval(output)
[{'itemkey2': 'item2', 'itemkey1': 'item1'}, {'itemkey2': 'item2', 'itemkey1': 'item1'}]

First of all, why not import the other script, and call a function from it to return you the data? That would return you a full python object instead of having to handle strings.

If you do want to pass structures around, I'd use the json module to serialize and deserialize the list of dictionaries:

print json.dumps(yourlist)

and

yourlist = json.loads(stdoutdata)

That'd make the output of your other script useful to more tools beyond Python.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top