1

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()
3
  • You might try opening the file in append "a" mode so that any previous files are not overwritten. Commented Feb 18, 2014 at 20:24
  • 1
    A few observations not directly related to your question: You assign to d but you do not use the result. if (not tokens in stopwords.words('english')) is not idiomatic Python. It would be better to write if tokens not in stopwords.words('english'). Commented Feb 18, 2014 at 20:28
  • 1
    even then he is evaluating for each i in tokens when it only depends on tokens as a whole ... likely a large slowdown\ Commented Feb 18, 2014 at 20:32

3 Answers 3

1
$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 = []
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe the . before $data, try this:

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

Regards!

1 Comment

@grphes, it didnt work. From cmd processData.py "my data" works, but it wouldnt work from php
0

There is no issue with the exec() or anything. The problem is that the nltk module is not able to locate the nltk_data directory. For it just locate where the nltk_data is present in your system: usually ~/nltk_data. Now import add that path when you run the function.

import nltk;

Now, nltk.data.path is a list of locations where to search for the modules. You can just do:

nltk.data.path.append("your location/directory");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.