我很难在python中解析数组时来自php文件。从PHP发送数组到Python,然后在Python中解析
test.php的
$count = 3;
$before_json = array("qwe","wer","ert");
print_r($before_json);
$after_json = json_encode($before_json);
echo $after_json;
$command = escapeshellcmd("test.py $after_json $count");
$output = shell_exec($command);
echo $output;
的print_r($ before_json);将显示 - Array([0] => qwe [1] => wer [2] => ert)
echo $ after_json;将显示 - [ “QWE”, “WER”, “ERT”]
py.test
import sys
after_json = sys.argv[1]
count = sys.argv[2]
print (device_id)
打印(after_json)将打印 - [QWE,疫情周报,ERT]
打印( after_json [1])将打印 - q
如何打印出after_json中的任何3个项目?
你忘了解析Python中的json。导入'json'模块,然后您可以使用'json.loads(after_json)'将JSON字符串转换为列表。 **更新**:我觉得这应该是一个答案,而不是一个评论,但我会留下评论,因为它已经有一些upvotes。 – rickdenhaan
首先让python程序执行你期望的操作 - 你只是读取一个字符串并从字符串中输出第一个字符......并尝试使用临时文件或管道在程序之间发送数据,而不是命令行 – MatsLindh