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.
<?php
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()
dbut you do not use the result.if (not tokens in stopwords.words('english'))is not idiomatic Python. It would be better to writeif tokens not in stopwords.words('english').i in tokenswhen it only depends on tokens as a whole ... likely a large slowdown\