如何获取 python 输出并使用 php 进行打印

发布于 2025-01-20 08:53:29 字数 1407 浏览 3 评论 0原文

这是我的python文件hi.py:

import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer

data = pd.read_csv("C:/xampp/htdocs/product.csv")
data['content'] = data['Brand']+' '+data['Title']+' '+data['Product Type']+' 
'+data['Gender']+' '+data['Price'].astype(str)+' '+data['MRP'].astype(str)+' 
'+data['Rating'].astype(str)

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features=1000)
product_vectors = vectorizer.fit_transform(data['content'].values)


from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(product_vectors)

def recommend(content):

 # find  index from dataset
 content_index = data[data['Title'] == content].index[0]

 # finding cosine similarities 
 distances = similarity[content_index]

# sorting cosine similarities
 my_list = sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[1:10]
 dh=[]
#return my_list
 for i in my_list:
    #print(data.iloc[i[0]].Title)
    dh.append(data.loc[i[0]].Title)
     #return dh
print(recommend('Adidas Men Running Shoes'))

我需要在PHP Localhost中打印的推荐产品列表。 这是我的PHP文件:

<?php
$output=shell_exec('py hi.py');
echo $output;
?>

但是,当我运行PHP文件时,我会在VSCODE中获得输出,但是在Localhost上运行时,什么也不会打印。

可能是什么问题,请帮助我。

This my python file hi.py:

import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer

data = pd.read_csv("C:/xampp/htdocs/product.csv")
data['content'] = data['Brand']+' '+data['Title']+' '+data['Product Type']+' 
'+data['Gender']+' '+data['Price'].astype(str)+' '+data['MRP'].astype(str)+' 
'+data['Rating'].astype(str)

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features=1000)
product_vectors = vectorizer.fit_transform(data['content'].values)


from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(product_vectors)

def recommend(content):

 # find  index from dataset
 content_index = data[data['Title'] == content].index[0]

 # finding cosine similarities 
 distances = similarity[content_index]

# sorting cosine similarities
 my_list = sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[1:10]
 dh=[]
#return my_list
 for i in my_list:
    #print(data.iloc[i[0]].Title)
    dh.append(data.loc[i[0]].Title)
     #return dh
print(recommend('Adidas Men Running Shoes'))

I need the list of recommended products to be printed in php localhost.
This is my php file:

<?php
$output=shell_exec('py hi.py');
echo $output;
?>

However when I'm running the php file I'm getting output in VSCode but nothing is printed when I run on localhost.

What might be the problem please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2025-01-27 08:53:29

问题在于您的php代码,例如它正在执行命令,但它仅将标准输出流返回给您,这就是错误的原因,您正在获得空白屏幕,因为shell_exec没有重定向错误。流,要这样做,如下所示,

<?php
$output=shell_exec('py hi.py 2>&1');
echo $output;
?>

一旦显示出错误,您将能够解决问题。

The problem is in your PHP code e.g. it's executing the command but it is only returning the standard output stream to you, that's the reason on error you are getting the blank screen because the shell_exec is not redirecting the error stream, to do so modify your code as shown below

<?php
$output=shell_exec('py hi.py 2>&1');
echo $output;
?>

Once the error is shown you will be able to solve your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文