Python的主要错误缺少快译通在LinkedIn的API调用 - 如何调用默认值呢? [英] Python Key Error Missing Dict in LinkedIn API call - how to call default value instead?

查看:120
本文介绍了Python的主要错误缺少快译通在LinkedIn的API调用 - 如何调用默认值呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话LinkedIn的API和使用json.loads到JSON对象转换为Python列表和类型的字典。有时候,某些LinkedIn个人资料,他们缺少的字典,如结束日期为他们的大学/教育(下面以粗体显示)。我会用什么机制来使其返回默认值(即N / A),而不是绊倒了失踪的字典键错误呢?我这样一个基本问题道歉,我刚开始使用Python / Django的。我真的AP preciate的帮助!

 高清家庭(要求):
    现在= datetime.datetime.now()
    HTML =< HTML和GT;<身体GT;
    令牌= oauth.Token(request.user.get_profile()组oauth_token,request.user.get_profile()。oauth_secret)
    客户端= oauth.Client(消费者,令牌)
    标题= {'X丽格式':'JSON'}
    URL_LIST = [绝密-URL,绝密-URL,绝密-URL]
在URL_LIST X:
    URL = \"http://api.linkedin.com/v1/people/url=\"+x+\":(id,first-name,last-name,headline,industry,three-current-positions,**educations:(end-date,**degree,field-of-study,school-name))\"
        RESP,内容= client.request(URL,GET,标题=标题)
        简介= json.loads(内容)
    HTML + =轮廓['的firstName'] ++简介['lastName的'] +:+简介['标题'] +|+简介['产业'] +|+简介['threeCurrentPositions '] ['值'] [0] ['公司'] ['名'] +|+简介['threeCurrentPositions'] ['值'] [0] ['标题'] +|+简介['的教育'] ['值'] [0] ['fieldOfStudy'] +|+简介['的教育'] ['值'] [0] ['度'] +|+简介['的教育'] ['值'] [0] ['schoolName'] +|** + STR(简介['的教育'] ['值'] [0] ['结束日期'] ['年']) ** +&下; p />中
    返回的Htt presponse(HTML)


解决方案

您可以使用 dict.get(键,默认)而不是字典[关键] 做一个字典查找时指定一个默认值。

I am calling the linkedin API and using json.loads to convert the JSON objects to Python lists and dicts. Sometimes for certain LinkedIn profiles, they are missing a dict such as the end date for their university/education (bolded below). What mechanism would I use to have it return a default value (i.e "N/A") instead of tripping up a "Key Error" for missing the dict? I apologize for such a basic question, I'm just starting out with Python / Django. I really appreciate the help!

def home(request):
    now = datetime.datetime.now()
    html = "<html><body>"
    token = oauth.Token(request.user.get_profile().oauth_token,request.user.get_profile().oauth_secret)
    client = oauth.Client(consumer,token)
    headers = {'x-li-format':'json'}
    url_list = ["redacted-URL","redacted-URL", "redacted-URL"]
for x in url_list:
    url = "http://api.linkedin.com/v1/people/url="+x+":(id,first-name,last-name,headline,industry,three-current-positions,**educations:(end-date,**degree,field-of-study,school-name))" 
        resp, content = client.request(url, "GET", headers=headers)
        profile = json.loads(content)
    html += profile['firstName'] + " " + profile['lastName'] + ": " + profile['headline'] + " | " + profile['industry'] + " | " + profile['threeCurrentPositions']['values'][0]['company']['name']+ " | " + profile['threeCurrentPositions']['values'][0]['title']+ " | " + profile['educations']['values'][0]['fieldOfStudy']+ " | " + profile['educations']['values'][0]['degree']+ " | " + profile['educations']['values'][0]['schoolName']+ " | " **+str(profile['educations']['values'][0]['endDate']['year'])** + "<p/>"
    return HttpResponse(html)

解决方案

You can use dict.get(key, default) instead of dict[key] to specify a default value when doing a dictionary lookup.

这篇关于Python的主要错误缺少快译通在LinkedIn的API调用 - 如何调用默认值呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆