[英]how to “php echo from python print”?
我正在嘗試將python矩陣轉換為php(努力解決此問題超過20個小時)。 我真的很感謝您的幫助!
下面有兩種情況:TEST和REAL。
它們具有與python運行完全相同的輸出以及相同的php代碼。 唯一的區別是,在REAL情況下,文件已在文件中放入python腳本。 TEST python本身運行良好,REAL python本身運行良好,TEST php-python運行正常,但REAL php-python運行失敗。 所以我的猜測是php不允許python將文件放入python腳本中?? 我的代碼如下:
===== test.py
import os, sys, json
g = json.loads( sys.argv[1] )
gene = ['>MhA1_Contig0.frz3.gene1', '>MhA1_Contig0.frz3.gene1', '>MhA1_Contig0.frz3.gene1']
gene.append(g)
seq = ["ATGGAC", "GGCACAGC", "CCACC"]
seq.append('ACTGAAA')
print json.dumps(output)
===== test.php
$output=null;
$g = "0.CUFF.6.1_2";
$data = escapeshellarg(json_encode($g));
exec("python /mysql/getSeq/test.py $data 2>&1 &", $output );
//exec("python /mysql/getSeq/test.py $data 2>&1", $output ); --> worked fine
//exec("python /mysql/getSeq/test.py $data", $output ); --> worked fine
//print_r($output); --> worked fine
//var_dump($output); --> worked fine
$result = json_decode( exec("python /mysql/getSeq/test.py $data", $output ) , true);
//print_r( $result); --> worked fine
//var_dump($result); --> worked fine
echo $result[0][0] . "<br>";
echo $result[0][1] . "<br>";
echo $result[1][0] . "<br>";
echo $result[1][1] . "<br>";
=====網頁上的結果
>MhA1_Contig0.frz3.gene1
ATGGAC
>MhA1_Contig0.frz3.gene1
GGCACAGC
===== real.py
import os, sys, json
inF = file("getSeq_Mh.fasta", "r")
g = json.loads( sys.argv[1] )
inF = file("getSeq_Mh.fasta", "r")
line = inF.readline()
matrix = []
--- (some codes here) ---
inF = file("getSeq_Mh.fasta", "r")
line = inF.readline()
gene = []
--- (some codes here) ---
for i in range(len(index)) :
output[i][0] = ">" + gene[i]
output[i][1]= str(matrix[i])[2:-4]
print json.dumps(output)
# print output
===== real.php
$output=null;
$g = "0.CUFF.6.1_2";
$data = escapeshellarg(json_encode($g));
exec("python /mysql/getSeq/real.py $data 2>&1 &", $output );
//exec("python /mysql/getSeq/real.py $data 2>&1", $output ); --> no difference in results
//exec("python /mysql/getSeq/real.py $data", $output ); --> no difference in results
$result = json_decode( exec("python /mysql/getSeq/real.py $data 2>&1", $output ) , true);
print_r($output) . "<br>";
//var_dump($output) . "<br>";
//print_r( $result) . "<br>";
//var_dump($result) . "<br>";
//echo $result[0][0] . "<br>";
//echo $result[0][1] . "<br>";
//echo $result[1][0] . "<br>";
//echo $result[1][1] . "<br>";
=====網頁上的結果
print_r($ output); ->錯誤
Array ( [0] => Traceback (most recent call last): [1] => File "/mysql/getSeq/getOne.py", line 3, in [2] => inF = file("getSeq_Mh.fasta", "r") [3] => IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta' [4] => Traceback (most recent call last): [5] => File "/mysql/getSeq/real.py.py", line 3, in [6] => inF = file("getSeq_Mh.fasta", "r") [7] => IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta' )
var_dump($ output); ->錯誤
array(8) { [0]=> string(34) "Traceback (most recent call last):" [1]=> string(95) " File "/mysql/getSeq/getOne.py", line 3, in " [2]=> string(38) " inF = file("getSeq_Mh.fasta", "r")" [3]=> string(63) "IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta'" [4]=> string(34) "Traceback (most recent call last):" [5]=> string(95) " File "/mysql/getSeq/real.py.py", line 3, in " [6]=> string(38) " inF = file("getSeq_Mh.fasta", "r")" [7]=> string(63) "IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta'" }
print_r($ result); ->錯誤
(nothing)
var_dump($ result); ->錯誤
NULL
[['>MhA1_Contig0.frz3.gene1', 'ATGGACT'], ['>MhA1_Contig0.frz3.gene1', 'GGCACAGC'], ['>MhA1_Contig0.frz3.gene1', 'CCACC']]
考慮使用PHP的proc_open()處理子處理(輸入/輸出); 這里的子進程是Python腳本。 下面在命令行中調用Python,傳遞$data
參數並接收$output
參數。
做出兩個可能需要最終調整的假設: python是服務器上的路徑變量,Python腳本與PHP腳本位於同一目錄中。 如果不是,請在命令行中插入python.exe的完整路徑,和/或在命令行中將目錄的完整路徑插入real.py。
$output=null;
$g = "0.CUFF.6.1_2";
$data = escapeshellarg(json_encode($g));
$cd = dirname(__FILE__); // CURRENT DIRECTORY OF SCRIPT
$output = null;
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", $cd."/error.txt", "a") // ERROR TXT OUTPUT OF PROCESSING
);
/* CALLING COMMAND LINE */
$process = proc_open("python \"$cd/real.py\" \"$data\"" , $descriptorspec, $pipes, $cd);
/* RECEIVING OUTPUT */
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
echo $output;
解決了 當我將所有文件(“ real.py”,“ real.php”和“ getSeq_Mh.fasta(已歸檔到real.py)”)放在同一目錄中時,它可以工作。
謝謝,凍糕。 您的稱贊給了我一個提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.