I would like to pass a PHP array to a Python script that will use the data to perform some tasks. I wanted to try to execute my Python script from PHP using shell_exec () and pass JSON data to it (which Iβm completely new to).
$foods = array("pizza", "french fries"); $result = shell_exec('python ./input.py ' . escapeshellarg(json_encode($foods))); echo $result;
The "escapeshellarg (json_encode ($ foods)))" function seems to pass my array as follows in a Python script (I get this value if I echo the function:
'["pizza","french fries"]'
Then inside the Python script:
import sys, json data = json.loads(sys.argv[1]) foods = json.dumps(data) print(foods)
This browser displays the following:
["pizza", "french fries"]
This is a simple old line, not a list. My question is: how can I best handle this data, such as a list or some kind of data structure that I can execute using "," as a separator? I really do not want to output the text to the browser, I just want to break the list into pieces and paste them into a text file in the file system.
source share