référence http://stackoverflow.com/questions/25845600/passing-php-array-to-python-doesnt-work
python
$numbers = [1, 2, 3, 4, 5];
$pythonScript = "./test.py";
$cmd = array("python", $pythonScript, escapeshellarg(json_encode($numbers)));
$cmdText = implode(' ', $cmd);
echo "Running command: " . $cmdText . "\n";
$result = shell_exec($cmdText);
var_dump(json_decode($result, true));
python
# -*- coding: utf-8 -*-
import numpy as np
import sys, json
try:
data = np.array(json.loads(sys.argv[1]))
std = np.std(data)
avg = np.mean(data)
min = np.min(data)
max = np.max(data)
result = {'std':std, 'avg':avg, 'min':min, 'max':max}
print json.dumps(result)
except Exception as e:
print str(e)
Recommended Posts