python脚本在php执行中显示错误

时间:2014-12-12 17:03:36

标签: php python

我正在使用php中的python script执行,但它显示错误ValueError: invalid literal for int() with base 10:'param2',而它从终端运行正常。

这是我的代码:

$String = "Hello there, how are you.";
$no_of_chars_per_word  = "3";
$special_word = "is";
$stop_list = "is are";

$param1 = $String;
$param2 = $no_of_chars_per_word;
$param3 = $special_word;
$param4 = $stop_list;

$command = "/usr/bin/python2.7 /home/anupam/public_html/Text_Analysis_Python/Total_word_count.py";
$command .=  " param1 param2 param3 param4 2>&1";

header('Content-Type: text/html; charset=utf-8');
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
echo "<style type='text/css'>
 body{
 background:#000;
 color: #7FFF00;
 font-family:'Lucida Console',sans-serif !important;
 font-size: 12px;
 }
 </style>";

$pid = popen( $command,"r");

echo "<body><pre>";
while( !feof( $pid ) )
{
 echo fread($pid, 256);
 flush();
 ob_flush();
 echo "<script>window.scrollTo(0,99999);</script>";
 usleep(100000);
}
pclose($pid);

echo "</pre><script>window.scrollTo(0,99999);</script>";
echo "<br /><br />Script finalizado<br /><br />";
?>

但是显示错误:

Traceback (most recent call last):
  File "/home/anupam/public_html/Text_Analysis_Python/Total_word_count.py", line 37, in 
    main()
  File "/home/anupam/public_html/Text_Analysis_Python/Total_word_count.py", line 17, in main
    min_char_per_word          = int(minimum_characters_per_word)
ValueError: invalid literal for int() with base 10: 'param2'


Script finalizado

在我的终端上运行正常:

anupam@JAZZ:~/public_html/Text_Analysis_Python$ python Total_word_count.py "hello there, how are you." "4" the "so the are"
3

这是我的python代码::

import sys

def main():
    """Accessing the command line arguments as per constraint"""
    test_data                   = sys.argv[1]       #string to be analysed
    minimum_characters_per_word = sys.argv[2]       #minimum characters per word to be analysed
    special_word                = sys.argv[3]       #special word to be analysed
    stop_list_string            = sys.argv[4]       #stop list to be ignored in the anaysis

    """Conversion of minimum characters per word to integer type"""
    min_char_per_word          = int(minimum_characters_per_word)
    """conversion of stop list as a string type to a list type"""
    stop_list = []                                  #stop list initially empty
    for words in stop_list_string.split():
        stop_list.append(words)                     #appending the stop list as per spaces between them


    total_word_count_res            = total_word_count(test_data,min_char_per_word,special_word,stop_list)
    print total_word_count_res

def total_word_count(test_data,min_no_chars,spec_words,stop_list):
    """Counts the total number of words in test data"""
    word_count = 0    
    for words in test_data.split():
        if len(words) >= min_no_chars and words not in(stop_list) :
            word_count += 1

    return word_count

if __name__ == '__main__':
    main()

我怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为

$command .=  " param1 param2 param3 param4 2>&1";

应该是

$command .=  ' "'.$param1.'" "'.$param2.'" "'.$param3.'" "'.$param4.'" 2>&1';

否则它只是给出了#34; paramX&#34;字符串。