Я использую выполнение python script
в php, но он показывает ошибку 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()
как я могу это разрешить?
я думаю что
$command .= " param1 param2 param3 param4 2>&1";
должно быть
$command .= ' "'.$param1.'" "'.$param2.'" "'.$param3.'" "'.$param4.'" 2>&1';
иначе он просто дает строки "paramX".
$command .= " param1 param2 param3 param4 2>&1";
заменит буквенные значения «param1» через «param4» значениями, которые эти переменные фактически содержат.