初级
我有一份json文件:
{'created_at':'Wed 9.27 01:19:39 +0000‘,'id':912849180741087232,'id_str':'912849180741087232','text':"RT @TheRickWilson:我看到点击服务正在生效,尖叫着AL中有特殊情况。\n\n,这是因为特朗普可以…“、“源”:“Twitter for iPhone”、“截断”:False、“in_reply_to_status_id”:None、‘in__to_status_id_str’:None、‘in_reply_to_user_id_id’、‘in_reply_to_user_id_str’、'in_reply_to_screen_name':None、‘in_to_status_id_str’:{id‘:66914769、id_str:'66914769',名称:'Kathy','screen_name':'mydoggigi','location':'Earth','url':None,'description':‘爱政治,孙子和PSU #StillWithHer #name,Susan Sarandon,Glenn Greenwald,Joel Osteen和Joe Scarborough!’translator_type:'none',‘’受保护:假,‘验证’:‘错误’,‘追随者_计数’:5878,‘朋友_计数’:5973,‘列表_计数’:143,‘热门_计数’:110285,‘状态_计数’:138191,‘创建_at’:‘08月19日:55:55:41+02009年’‘,’utc_偏移‘:-14400,’时区‘:’东部时间(美国和加拿大)‘,'geo_enabled':True,'lang':'en',’贡献者_启用‘:False,'is_translator':False,’profile__本色‘:'C0DEED',“配置文件_背景_图像_url”:'http://abs.twimg.com/images/themes/theme1/bg.png',‘配置文件_背景_图像_url_https’:'https://abs.twimg.com/images/themes/theme1/bg.png',‘概要文件_背景_瓷砖’:False,'profile_link_color':'1DA1F2',‘profile_侧边栏_边框_True_True’:'C0DEED',‘profile_侧栏_fill_color’:‘DDEEF6 6’,'profile_text_color':'333333',‘profile_use_fill_tile_image’:True,'profile_image_url':'normal.jpg','profile_image_url_https':'normal.jpg','profile_banner_url':'banners/66914769/1504225271','default_profile':True,‘default_profile_banners/66914769/1504225271’:False,‘banner’:None,‘None,’通知‘:None},'geo':None,’坐标‘,’place‘,’None‘,’贡献者‘:无,“转发状态”:{'created_at':'Wed 927 01:08:45 +0000 2017','id':912846439964987392,'id_str':'912846439964987392','text':“我看到点击服务正在生效,尖叫着在AL有特殊情况。\n\n,这是因为特朗普无法提供。”“悲伤!”,“源”:'Twitter for Android','truncated':False,‘in_reply_to_status_id_id’:None,'in_reply_to_status_id_str':None,‘in_str_to_user_id_id’:None,‘in_str_to_user_id_str’:None,‘in_str_to_screen_name’:None,'user':{id‘:19084896,'id_str':'19084896',“名字”:“里克·威尔逊”,“屏幕名”:“TheRickWilson”,“location”:“佛罗里达州和其他地方”,“url”:“http://facebook.com/therickwilson”,“描述”:“共和党媒体盖伊,爸爸,丈夫,飞行员,亨特,作家”。我做广告做政治。每日野兽专栏作家。“特朗普触碰到的一切。”、“翻译器类型”:“无”、“保护”:假、“验证”:真、“追随者_计数”:238578、“朋友_计数”:3518、‘列表_计数’:4235、‘热门_计数’:48094、‘statuses_count’、250609、'created_at':'Fri Jan 16 20:50:17 +0000 2009‘、’utc_偏移‘:-14400、'time_zone':'America/New_York',‘'geo_enabled':False,'lang':'en',’贡献者_启用‘:False,’profile_背景_color‘:'1A1B1F',’配置文件_背景_图像_url_url‘:'Wallpaper.jpg','Wallpaper.jpg',’profile_本底_瓦‘:True,'profile_link_color':'445555',’配置文件_侧栏_边界_Wallpaper.jpg‘:'000000',“配置文件_侧边栏_填充_颜色”:'252429',‘配置文件_文本_颜色’:'666666',‘配置文件_use_背景_图像’:True,'profile_image_url':'normal.jpg','profile_image_url_https':'normal.jpg','profile_banner_url':'banners/19084896/1504722796',‘default_profile_banners/19084896/1504722796’:False,'default_profile_image':False,‘None,’sent_request_sent‘:’无,“通知”:None},“geo”:None,“坐标”:None,“place”:None,'is_quote_status':False,‘引号_count’:5,'reply_count':50,'retweet_count':100,‘收藏_count’:456,‘实体’:{‘hashtag’:[],'urls':[],‘user_status’::[],‘符号’:[]},“收藏夹”:False,“retweet”:False,'filter_level':'low','lang':'en'},'is_quote_status':False,‘引号’:0,‘user_count’:0,'retweet_count':0,‘收藏_计数’:0,'entities':{‘hashtag’:[],'urls':[],‘user_quote’:[{'screen_name':'TheRickWilson',‘TheRickWilson’,‘'name':'Rick’,'id':19084896,'id_str':'19084896',‘id’:3,17}],‘符号’:[]},‘收藏’: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文件的粗体部分)
有人知道怎么做吗?谢谢你!!
发布于 2017-10-20 02:22:46
可以访问嵌套字典,如下所示:
data['user']['name']发布于 2017-10-20 02:37:51
如果您确信“user”键将始终存在于您的json文件中,那么您可以先这样做:
user = data['user']如果您知道用户一定会有一个名称,那么您可以像下面这样读取“name”字段:
return user['name']将上述两个步骤合并为一个步骤,只需完成以下操作:
return data['user']['name']如果您希望“user”字段有时不存在,您可以通过不同的方法来处理它:
# 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']https://stackoverflow.com/questions/46841466
复制相似问题