php调用python脚本快速读取excel_如何从php调用python脚本

I need to pass a value from PHP to a Python script which then writes the value into a csv file. But, I'm having difficulties, my python when called writes an empty csv file. What could be the problem.

if (isset($_POST['data'])){

$data = $_POST['data'];

$result = exec("python processData.py .$data");

echo $result;

}

?>

and processData.py

import nltk

from nltk.corpus import stopwords

from nltk import stem

import re

import sys

import csv

mysentence = sys.argv[1]

f = open("output.csv", "wb")

tokens = nltk.word_tokenize(mysentence)

d = [i.lower() for i in tokens if (not tokens in stopwords.words('english'))]

porter = nltk.PorterStemmer()

for t in tokens:

result = porter.stem(t)

f.write(result+"\n")

print result

f.close()

解决方案$result = exec("python processData.py .$data");

is likely the problem if you typed : $data = "hello little world";

it woudl pass as

$result = exec("python processData.py .hello little world");

sys.argv would be

["processData.py",".hello","little","world"]

unfortunately im not sure how nltk would handle that but surely not as you are intending

as an aside

d = [i.lower() for i in tokens if (not tokens in stopwords.words('english'))]

should be rewriten

if tokens not in stopwords.words('english'):

d = [i.lower() for i in tokens]

else: #if your actually planning on using d anywhere ... currently your just throwing it out

# not using d makes all of this just as effective as a pass statement

d = []

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值