Way to make terminal calls in Python, capture what is returned? -


i'm working on project right i'm passing in strings nlp api, returns json objects of string's sentiment analysis. admit python newbie:

http://text-processing.com/docs/sentiment.html

the documentation calling api simple through command line. works fine when open terminal , run command.

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

running command on terminal produces:

{"probability": {"neg": 0.59309571705919506, "neutral": 0.5403849950008478, "pos": 0.40690428294080488}, "label": "neutral"} 

i trying figure out way in python make terminal call using same command, , capturing json object, decoding it, , using in code.

so far, i've found using below code works in python:

import os os.system('curl -d "text=great" http://text-processing.com/api/sentiment/') 

however, when run line in python file, prints out json object. how can save output variable, , dump string , use json result in code?

when try:

import os sentiment = os.system('curl -d "text=great" http://text-processing.com/api/sentiment/') 

it ignores variable assignment, , proceeds print out json object.

any suggestions?

there couple of ways this. method similar code use subprocess package make calls operating system.

import subprocess process = subprocess.popen(['curl', '-d', '"text=great"',                             'http://text-processing.com/api/sentiment/'],                              stdout=subprocess.pipe) stuff, err = process.communicate() 

the other way use python package make post request.

import requests response = requests.post(url='http://text-processing.com/api/sentiment/',                          data="text=great").content stuff = response.content 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -