这是我到目前为止的代码
$dataraw = $_SESSION['image'];
$datagambar = json_encode($dataraw);
echo '<pre>';
print_r($dataraw);
echo '</pre>';
print($escaped_json);
$type1 = gettype($dataraw);
print($type1);
$type2 = gettype($datagambar);
print($type2);
Run Code Online (Sandbox Code Playgroud)
这是$dataraw输出,类型是数组
Array
(
[0] => Array
(
[FileName] => 20221227_202035.jpg
[Model] => SM-A528B
[Longitude] => 106.904251
[Latitude] => -6.167665
)
[1] => Array
(
[FileName] => 20221227_202157.jpg
[Model] => SM-A528B
[Longitude] => 106.9042428
[Latitude] => -6.1676580997222
)
)
Run Code Online (Sandbox Code Playgroud)
这是 $datagambar 输出,类型是字符串
[{"FileName":"20221227_202035.jpg","Model":"SM-A528B","Longitude":106.904251,"Latitude":-6.167665},{"FileName":"20221227_202157.jpg","Model":"SM-A528B","Longitude":106.9042428,"Latitude":-6.167658099722223}]
Run Code Online (Sandbox Code Playgroud)
传递给Python
echo shell_exec("D:\Anaconda\python.exe D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py $datagambar");
Run Code Online (Sandbox Code Playgroud)
这是我的 python test.py
import sys, json
import os
import pymysql
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplcursors as mpl
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score,hamming_loss,classification_report
json_list = []
escaped_json1 = sys.argv[1]
# this is working but its only a string of array json
# print(escaped_json1)
# this is working but its only a string of array json
json_list.append(json.loads(escaped_json1))
parsed_data = json.loads(escaped_json1)
print(json_list)
print(parsed_data)
Run Code Online (Sandbox Code Playgroud)
当我执行 print(escaped_json1) 时,它会显示来自 php($datagambar) 的 json 数组字符串。
蟒蛇输出:
Hello world has been called [{"FileName":"20221227_202035.jpg","Model":"SM-A528B","Longitude":106.904251,"Latitude":-6.167665},{"FileName":"20221227_202157.jpg","Model":"SM-A528B","Longitude":106.9042428,"Latitude":-6.167658099722223}]
Run Code Online (Sandbox Code Playgroud)
我使用 apache 作为我的服务器,并使用 phpmyadmin 和 anaconda。
T 尝试使用 print(type(escapedjson1)) 或 print(type(escapedjson1)) 但它不显示类型
json.loads 没有将数据类型更改为 python 数组
如何加载它并将字符串数组放入变量数组中,以便我可以调用它并将其转换为数据帧?
更新:完全不同的方法
PHP 脚本对一个结构进行 JSON 编码以生成 JSON 字符串,然后将其作为命令行参数传递,这是一个困难,因为该字符串需要放在双引号中,因为编码字符串中可能会嵌入空格。但字符串本身可以包含双引号作为字符串字符的开头并在该字符串内。使困惑?谁不会呢?
然而,将这样的字符串写入文件并让 Python 程序读入并解码它是没有问题的。但我们不想处理临时文件。所以解决方案是将数据通过管道传输到 Python 程序,然后可以将其作为stdin读入
假设您的数组如下所示:
$arr =
[
[
"FileName" => "\"\\ \nRon's20221227_202035.jpg",
"Model" => "27_202035.jpg",
"Longitude" => 106.90425,
"Latitude" => 106.90425
],
[
"FileName" => "20221227_202157.jpg",
"Model" => "SM-A528B",
"Longitude" => 106.9042428,
"Latitude" => -6.1676580997222
]
];
Run Code Online (Sandbox Code Playgroud)
请注意,我稍微修改了您的示例,以便第一个FileName字段包含一个"字符、一个'字符、一个\n表示换行符的转义序列,最后是一些空格。尽管您的示例不包含这些字符或其他一些转义序列,但我希望能够在出现这种情况时处理它。该解决方案应该适用于此类输入。
PHP文件
<?php
$arr =
[
[
"FileName" => "\"\\ \nRon's20221227_202035.jpg",
"Model" => "27_202035.jpg",
"Longitude" => 106.90425,
"Latitude" => 106.90425
],
[
"FileName" => "20221227_202157.jpg",
"Model" => "SM-A528B",
"Longitude" => 106.9042428,
"Latitude" => -6.1676580997222
]
];
// Encode string as JSON:
$json = json_encode($arr);
// Pipe the JSON string to the Python process's stdin and
// read the result from its stdout:
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w") // stdout is a pipe that the child will write to
);
$options = array('bypass_shell' => True); // Only has effect on Windows
//$process = proc_open("python3 test.py", $descriptorspec, $pipes, null, null, $options);
// For your actual environment:
$process = proc_open("D:/Anaconda/python.exe D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py", $descriptorspec, $pipes, null, null, $options);
// Pipe the input for the Python program and close the pipe:
fwrite($pipes[0], $json);
fclose($pipes[0]);
// Read the result from the Python program and close its pipe
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
# Now that we have closed the pipes, we can close the process:
$return_code = proc_close($process);
echo "Result from stdin:\n$result\n";
Run Code Online (Sandbox Code Playgroud)
测试.py
import json
import sys
DEBUG = True # for debugging purposes
if DEBUG:
import os
for k, v in sorted(os.environ.items()):
print(k, '->', v, file=sys.stderr)
else:
import numpy as np
import pandas as pd
# Load from stdin:
arr = json.load(sys.stdin)
# print each dictionary of the array:
for d in arr:
print(d)
Run Code Online (Sandbox Code Playgroud)
印刷:
{'FileName': '"\\ \nRon\'s20221227_202035.jpg', 'Model': '27_202035.jpg', 'Longitude': 106.90425, 'Latitude': 106.90425}
{'FileName': '20221227_202157.jpg', 'Model': 'SM-A528B', 'Longitude': 106.9042428, 'Latitude': -6.1676580997222}
Run Code Online (Sandbox Code Playgroud)