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