Displaying JSON specific JSON result in Python
Question: I'm new with Python and have the following code:

    
    
    def doSentimentAnalysisAndPrint(keyval):  
        import urllib
    
        data = urllib.urlencode(keyval) 
        u = urllib.urlopen("http://text-processing.com/api/sentiment/", data)
        json_string = u.read()   
    
        parsed_json = json.loads(json_string)
    
        # print the various key:values
        print(parsed_json['probability'])
        print ">>", parsed_json['label']
    

The printed result is:

    
    
    {u'neg': 0.24087437946650492, u'neutral': 0.19184084028194423, u'pos': 0.7591256205334951}
    >> pos
    

I would like to only print out the actual result? E.G. in this case "Positive:
0.7591256205334951" but don't know how to achieve this?


Answer: Do read the [API documentation](http://text-
processing.com/docs/sentiment.html) when using one. The `'label'` key points
to what key in the `'probability'` dictionary is the determined sentiment:

> **label:** will be either `pos` if the text is determined to be _positive_ ,
> `neg` if the text is _negative_ , or `neutral` if the text is neither `pos`
> nor `neg`.
>
> **probability:** an object that contains the probability for each label.
> `neg` and `pos` will add up to 1, while `neutral` is standalone. If
> `neutral` is greater than `0.5` then the `label` will be `neutral`.
> Otherwise, the `label` will be `pos` or `neg`, whichever has the greater
> probability.

So you already have a label, and the corresponding value is just a key lookup.
Map the label values to a string to print (like `pos` mapping to `Positive`,
and combine the two:

    
    
    sentiments = {'pos': 'Positive', 'neg': 'Negative', 'neutral': 'Neutral'}
    label = parsed_json['label']
    print sentiments[label], parsed_json['probability'][label]
    

