簡體   English   中英

如何使用 shell_exec() 從 python 文件格式化 output

[英]How to format output from a python file using shell_exec()

我目前有從 python 文件輸出數據的 php 代碼。 這是運行成功的output。

Total = 11 Number of Correct Answers = 4 Percentage Correct = 36.36363636363637 % Number of Incorrect Answers = 7 Percentage Wrong = 63.63636363636363 % [['0' '1' '2'] ['0' '3' '13'] ['0' '4' '14'] ['0' '2' '8'] ['1' '2' '6'] ['0' '4' '18'] ['1' '1' '2'] ['0' '2' '7'] ['0' '3' '14'] ['0' '2' '7'] ['0' '1' '2']] [1 0 1 0 0 0 1 0 0 1 0] probabilities [[0.47642793 0.52357207] [0.94137447 0.05862553] [0.95579032 0.04420968] [0.81265167 0.18734833] [0.7674815 0.2325185 ] [0.98345533 0.01654467] [0.53448523 0.46551477] [0.77109059 0.22890941] [0.95386825 0.04613175] [0.77109059 0.22890941] [0.47642793 0.52357207]]

我不知道如何將這個 output 格式化回我的 php 頁面。 顯然,當您在終端中運行 py 文件時,它的格式非常完美。 我想要有適當的換行符。 就像是

Total = 11
Number of Correct Answers

Percentage Correct.

我試圖在實際的 py 文件中實現換行,但是當 php 文件從 py 文件輸出數據時,它總是出現在上述場景中。

這是我的代碼:

php-

<?php
                
              $email = $_SESSION['email'];
                
              
              $command = escapeshellcmd("C:/Python38/python.exe C:/xampp/htdocs/Ensemble/login/test.py $email"); 
              $output = shell_exec("$command 2>&1");
              print($output);
             
              
            ?>

py:

print("Total =", len(training_set)) 

print("Number of Correct Answers =", len(correct))
print("Percentage Correct =", 1.*len(correct)/len(training_set)*100.0, "%")
 
print("Number of Incorrect Answers =", len(incorrect))
print("Percentage Wrong =", 1.*len(incorrect)/len(training_set)*100.0, "%")

(矩陣有更多輸出,但我試圖首先關注這些 py 元素)。

HTML 不關心 \n (新行),您必須使用<br>移動到下一行。

您可以在每個print()中將其添加到 Python

print("Total =", len(training_set), '<br>') 

或者您可以使用 nl2br 替換PHP

print( nl2br($output) );

最終你可以在<code></code><pre></pre>中顯示文本

<pre>

<?php
    print( $output );
?>

</pre>

它會尊重\n但我不記得你是否可以在里面使用其他 HTML 標簽來格式化它。

它還將像終端一樣使用等寬字體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM