我很難在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