为JSON文件中的每一行运行终端命令

时间:2017-01-05 03:45:44

标签: python json subprocess

我有一个json文件,其中包含从Tweepy挖掘的推文。为了获得每条推文的情绪分析,我试图使用www.text-processing.com上的API。您可以在下面查看我现在的(有缺陷的)代码。

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

我推测这主要是因为我使用format()函数来命令通过终端(通过子进程模块)。我想在我的json文件中为每条推文运行的终端命令是:

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

有谁知道一个好的解决方案?谢谢!

1 个答案:

答案 0 :(得分:1)

bash_com等于'curl -d "text={}".format(tweet_words) http://text-processing.com/api/sentiment/'

你没有调用format因为它在字符串里面。

你不应该这样做,因为它不安全,你可以直接使用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})
相关问题