2017-01-05 2 views
0

Tweepyからマイニングされたつぶやきを含むjsonファイルがあります。すべてのツイートについて感情分析を行うために、私はwww.text-processing.comからAPIを使用しようとしています。私の現在の(欠陥のある)コードを以下で見ることができます。JSONファイルのすべての行にターミナルコマンドを実行

fname = 'python.json' 
with open(fname, 'r') as f: 
    for line in f: 
     tweet = json.loads(line) 
     # Draw out the text from each tweet 
     tweet_words = tweet['text'] 
     bash_com = 'curl -d "text={}".format(tweet_words) http://text-processing.com/api/sentiment/' 
     subprocess.Popen(bash_com) 
     output = subprocess.check_output(['bash','-c', bash_com]) 
     with open('sentiment.json', 'w') as s: 
      s.write(output) 

それは次のエラーを返します。

CalledProcessError: Command '['bash', '-c', 'curl -d "text={}".format(tweet_words) http://text-processing.com/api/sentiment/']' returned non-zero exit status 1 

私はサブプロセスモジュールを経由して端末を通過すると仮定されたコマンドの形式()関数を(使用していますので、これは主に推測)。私のjsonファイル内のすべてのツイートのために実行したいターミナルコマンドは:

curl -d "text=word" http://text-processing.com/api/sentiment/ 

良い解決法を知っている人はいますか?ありがとう!

答えて

1

bash_comは、それが文字列の中だからあなたがformatを呼び出していない'curl -d "text={}".format(tweet_words) http://text-processing.com/api/sentiment/'

に等しいです。

あなたはそれが安全でないなので、とにかく、あなたが直接subprocess.check_output使用できることをやるべきではありません。

output = subprocess.check_output(['curl', '-d', "text=" + tweet_words, 'http://text-processing.com/api/sentiment/']) 

しかし、あなたがそのような代わりに、要求としてHTTPライブラリを使用する必要があります。

requests.post('http://text-processing.com/api/sentiment/', {'text': tweet_words}) 
+0

感謝を!このソリューションは機能します! – Michael

関連する問題