2017-10-20 51 views
0

初級調用字典內的字典的值(讀取JSON文件)

我有此JSON文件:

{ 'created_at':「星期三09月27 1點19分39秒+ 0000 2017','id':912849180741087232,'id_str':'912849180741087232','text':「RT @TheRickWilson:我發現點擊撲克已經生效,尖叫聲中有特殊情況。\ n \ n是的,是因爲Trump ca ...「,」source「:'Twitter for iPhone','截斷':False,'in_reply_to_status_id':無,'in_reply_to_status_id_str':無,'in_reply_to_user_id':無,'in_reply_to_user_id_str':無,'in_reply_to_screen_name':無,'user':{'id':66914769,'id_str':'66914769','name':'Kathy','screen_name':'mydoggigi','location':'Earth','url' 'description':'愛情政治,孫子& PSU #StillWithHer #NotMyPresident蘇珊莎蘭登,格倫格林沃爾德,喬爾奧斯汀和喬斯卡伯勒阻止! #TheResistance','translator_type':'none','protected':False,'verified':False,'followers_count':5878,'friends_count':5973,'listed_count':143,'favourites_count':110285,'' '','','','','','','','',' 'lang':'en','contributors_enabled':False,'is_translator':False,'profile_background_color':'C0DEED','profile_background_image_url':'http://abs.twimg.com/images/themes/theme1/bg.png','profile_background_image_url_https':'https://abs.twimg.com/images/themes/theme1/bg.png','profile_background_tile':False, 'profile_link_color':'1DA1F2','profile_sidebar_border_color':'C0DEED','profile_sidebar_fill_color':'DDEEF6','profile_text_color':'333333','profile_use_background_image':True,'profile_image_url':'http://pbs.twimg.com/profile_images/903412377424732160/NqCfPFiB_normal.jpg','profile_image_url_https': 'https://pbs.twimg.com/profile_images/903412377424732160/NqCfPFiB_normal.jpg','profile_banne r_url':'https://pbs.twimg.com/profile_banners/66914769/1504225271','default_profile':True,'default_profile_image':False,'following':None,'follow_request_sent':None,'notifications':None},'geo':None,'coordinates':None, 'place':None,'contributors':None,'retweeted_status':{'created_at':'Wed Sep 27 01:08:45 +0000 2017','id':912846439964987392,'id_str':'912846439964987392','文字':「我發現點滴已經生效,尖叫着AL的特殊情況。\ n \ n是的,這是因爲特朗普無法交付。 '','source':'Twitter for Android','truncated':False,'in_reply_to_status_id':None,'in_reply_to_status_id_str':None,'in_reply_to_user_id':None,'in_reply_to_user_id_str':None,'in_reply_to_screen_name':None, 'user':{'id':19084896,'id_str':'19084896','name':'Rick Wilson','screen_name':'TheRickWilson','location':'Florida and points beyond','url' :'http://facebook.com/therickwilson','description':'GOP Media Guy,爸爸,丈夫,飛行員,獵人,作家,我做廣告和做政治,每日野獸專欄作家,一切特朗普接觸死亡。','translator_type':'none','protected':False,'verified':True,'followers_count':238578,'friends_count':3518,'listed_count':4235,'favourites_count':48094,'statuses_count': 250609,'created_at':'Fri Jan 16 20:50:17 +0000 2009','utc_offset':-14400,'time_zone':'America/New_York','geo_enabled':False,'lang':'en' ,'contributors_enabled':False,'is_translator':False,'profile_background_color':'1A1B1F','profile_background_image_url':'http://pbs.twimg.com/profile_background_images/220716353/Firefox_Wallpaper.jpg','profile_background_image_url_https':'https://pbs.twimg.com/profile_background_images/220716353/Firefox_Wallpaper.jpg','profile_background_tile':True,'profile_link_color':'445555' 'profile_image_url_https':'https://pbs.twimg.com/profile_images/813585115934658560/gnuRozoD_normal.jpg','profile_banbar_color':' :'https://pbs.twimg.com/profile_banners/19084896/1504722796','default_profile':False,'default_profile_image': False,'following':None,'follow_request_sent':None,'notifications':None},'geo':None,'coordinates':None,'place':None,'貢獻者':None,'is_quote_status':False ,'quote_count':5,'reply_count':50,'retweet_count':100,'favorite_count':456,'entities':{'hashtags':[],'urls':[],'user_mentions':[] ,'symbols':[]},'favitedited':False,'retweeted':False,'filter_level':'low','lang':'en'},'is_quote_status':False,'quote_count':0, 'reply_count':0,'retweet_count':0,'favorite_count':0,'entities':{'hashtags':[],'urls':[],'user_mentions':[{'screen_name':'TheRickWilson' ,'name':'Rick Wilson','id':19084896,'id_str':'19084896','indices':[3,17]]],'symbols':[]},'favorited':False, 'retweeted':False,'filter_level':'low','lang':'en','timestamp_ms':'1506475179263'}

我能弄清楚如何調用部分json文件中像這樣:

with open('trump1.json') as data_file: 
     #making a dictionary of the json document so I can call values 
    data = json.load(data_file) 
    #print(data) 
    #when the tweet was made 
    data["created_at"] 
    print(data["created_at"]) 
    #content of the tweet 
    data["text"] 
    print(data["text"]) 

但我無法弄清楚如何調用的重要信息,如

data["name"] 

以來的字典的一個內(見JSON文件的加粗部分)

有誰知道如何做到這一點? 謝謝!

+0

你有什麼嘗試?關於這個和外面的問題有很多問題。 – Antimony

回答

1

您可以訪問嵌套的字典,像這樣:

data['user']['name'] 
+0

非常感謝! – Brit

0

如果你肯定知道「用戶」關鍵是要出現在你的JSON文件總是,那麼你可以先做:

user = data['user'] 

如果您知道用戶肯定會有一個名字,那麼你可以閱讀「名稱」字段是這樣的:

return user['name'] 

上述兩個步驟合併爲一個,你可以這樣做:

return data['user']['name'] 

如果你希望「用戶」字段有時不存在,也有不同的方法可以處理它:

# Using the dictionary object's .get() method to be safe. 
# Here, returning None by default. 
# This isn't a good idea if None is an expected value for name. 
return data.get('user', {}).get('name') 

# Surrounding the dictionary lookup with try ... except 
# Of course, KeyError is just one of the ways this can go wrong. 
try: 
    return data['user']['name'] 
except KeyError: 
    # Handle error 

# Look before you leap. Verify that 'user' and 'name' fields are present. 
# Though, this results in two dictionary lookups. 
if 'user' in data: 
    user = data['user'] 
    if 'name' in user: 
     return user['name']