2016-05-28 45 views
1

我想使用Python解析出一個JSON下載,在這裏來自IBM沃森解析JSON是下載,我有:錯誤而使用python

{ 
    "document_tone":{ 
     "tone_categories":[ 
     { 
      "tones":[ 
       { 
        "score":0.044115, 
        "tone_id":"anger", 
        "tone_name":"Anger" 
       }, 
       { 
        "score":0.005631, 
        "tone_id":"disgust", 
        "tone_name":"Disgust" 
       }, 
       { 
        "score":0.013157, 
        "tone_id":"fear", 
        "tone_name":"Fear" 
       }, 
       { 
        "score":1.0, 
        "tone_id":"joy", 
        "tone_name":"Joy" 
       }, 
       { 
        "score":0.058781, 
        "tone_id":"sadness", 
        "tone_name":"Sadness" 
       } 
      ], 
      "category_id":"emotion_tone", 
      "category_name":"Emotion Tone" 
     }, 
     { 
      "tones":[ 
       { 
        "score":0.0, 
        "tone_id":"analytical", 
        "tone_name":"Analytical" 
       }, 
       { 
        "score":0.0, 
        "tone_id":"confident", 
        "tone_name":"Confident" 
       }, 
       { 
        "score":0.0, 
        "tone_id":"tentative", 
        "tone_name":"Tentative" 
       } 
      ], 
      "category_id":"language_tone", 
      "category_name":"Language Tone" 
     }, 
     { 
      "tones":[ 
       { 
        "score":0.0, 
        "tone_id":"openness_big5", 
        "tone_name":"Openness" 
       }, 
       { 
        "score":0.571, 
        "tone_id":"conscientiousness_big5", 
        "tone_name":"Conscientiousness" 
       }, 
       { 
        "score":0.936, 
        "tone_id":"extraversion_big5", 
        "tone_name":"Extraversion" 
       }, 
       { 
        "score":0.978, 
        "tone_id":"agreeableness_big5", 
        "tone_name":"Agreeableness" 
       }, 
       { 
        "score":0.975, 
        "tone_id":"emotional_range_big5", 
        "tone_name":"Emotional Range" 
       } 
      ], 
      "category_id":"social_tone", 
      "category_name":"Social Tone" 
     } 
     ] 
    } 
} 

我試圖分析出從'tone_name''score'上面的文件,我使用下面的代碼:

import urllib 
import json 
url = urllib.urlopen('https://watson-api-explorer.mybluemix.net/tone-analyzer/api/v3/tone?version=2016-05-19&text=I%20am%20happy') 
data = json.load(url) 
for item in data['document_tone']: 
    print item["tone_name"] 

我一直運行到該tone_name沒有定義的錯誤。

+0

您的代碼不符合你解析JSON的結構。 'data ['document_tone']'是一個字典,但'tone_name'是結構中字典的一個關鍵字。 – jonrsharpe

+0

我的回答有幫助嗎?如果沒有,請告訴我爲什麼我可以編輯它。 –

回答

0

由於jonrsharpecomment說:

data['document_tone']是一本字典,但'tone_name'是在詞典中的一個關鍵多少進一步下跌的結構。

您需要訪問字典,tone_name是,如果我理解正確的JSON,tone_name是內tones的關鍵,內tone_categories,內document_tone。那麼你想改變你的代碼到那個水平,像這樣:

for item in data['document_tone']['tone_categories']: 
    # item is an anonymous dictionary 
    for thing in item[tones]: 
     print(thing['tone_name']) 

的原因多了一個比需要的是因爲列表和字典的文件中混合。 'tone_categories是一個詞典列表,所以它訪問每一個。然後,它遍歷列表tones,每個列表中都有更多字典。這些字典是包含'tone_name'的字典,因此它打印的值爲'tone_name'

如果這不起作用,請告訴我。我無法測試它,因爲我無法讓其餘的代碼在我的電腦上工作。

0

你錯誤地走了結構。根節點具有單個密鑰document_tone,其中的值僅具有tone_categories密鑰。每個類別都有一個聲調列表,它的名稱。這裏是你如何把它打印出來(根據需要調整):

for cat in data['document_tone']['tone_categories']: 
    print('Category:', cat['category_name']) 
    for tone in cat['tones']: 
     print('-', tone['tone_name']) 

這樣做的結果是:

Category: Emotion Tone 
- Anger 
- Disgust 
- Fear 
- Joy 
- Sadness 
Category: Language Tone 
- Analytical 
- Confident 
- Tentative 
Category: Social Tone 
- Openness 
- Conscientiousness 
- Extraversion 
- Agreeableness 
- Emotional Range 
+0

很高興我能幫到你。如果問題得到徹底解決,請接受答案(點擊評分下面的複選標記)。 – Anonymous