如何获取 python 输出并使用 php 进行打印
这是我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的php代码,例如它正在执行命令,但它仅将标准输出流返回给您,这就是错误的原因,您正在获得空白屏幕,因为
shell_exec
没有重定向错误。流,要这样做,如下所示,一旦显示出错误,您将能够解决问题。
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 belowOnce the error is shown you will be able to solve your issue.