【问题标题】:How to format output from a python file using shell_exec()如何使用 shell_exec() 格式化 python 文件的输出
【发布时间】:2020-06-24 02:18:55
【问题描述】:

我目前有从 python 文件输出数据的 php 代码。这是成功运行的输出。

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]]

我不知道如何在我的 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 显示它,那么你必须用 &lt;br&gt; 代替 \n - 因为 HTML 不关心 \n(新行) - 你可以在PHP 与 nl2br。或者您必须在 &lt;code&gt; your text &lt;/code&gt; 中显示它才能将 \n 显示为新行。

标签: python php formatting backend web-development-server


【解决方案1】:

HTML 不关心 \n(新行),您必须使用 &lt;br&gt; 移动到下一行。

你可以在每个print()中添加Python

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

或者您可以在 PHP 中使用 nl2br 替换

print( nl2br($output) );

最终您可以在&lt;code&gt;&lt;/code&gt;&lt;pre&gt;&lt;/pre&gt; 中显示文本

<pre>

<?php
    print( $output );
?>

</pre>

它会尊重\n,但我不记得你是否可以在里面使用其他HTML标签来格式化它。

它也会像终端一样使用等宽字体。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多