使用JSON和字典处理Python中的错误 [英] Error handling in Python with JSON and a dictionary
问题描述
{
id:4,
name:Mark Zuckerberg,
first_name:Mark,
last_name:Zuckerberg,
link:http://www.facebook 。
username:zuck,
gender:male,
locale:en_US
}
JSON值可能因网页而异。上述示例列出了所有可能性,但有时候,诸如用户名之类的值可能不存在,并且可能会遇到JSON数据,例如: -
{
id:6,
name:Billy Smith,
first_name:Billy,
last_name Smith,
gender:male,
locale:en_US
}
使用这些数据,我想填充数据库表。因此,我的代码如下: -
results_json = simplejson.loads(scraperwiki.scrape(profile_url))
for result in results_json:
profile = dict()
try:
profile ['id'] = int(results_json ['id'])
除了:
profile ['id'] =
try:
profile ['name'] = results_json ['name']
除了:
profile ['name'] =
try:
profile ['first_name'] = results_json ['first_name']
除了:
profile ['first_name'] =
try:
profile ['last_name'] = results_json ['last_name']
除了:
profile ['last_name'] =
try:
profile ['link '] = results_json ['link']
除了:
个人资料['link'] =
try:
个人资料['username'] = results_json ['username']
除了:
profile ['username'] =
try:
profile ['gender'] = results_json ['gender']
except:
profile ['gender'] =
try:
profile ['locale'] = results_json ['locale']
除了:
profile ['locale'] =
我有这么多尝试/除外的原因是说明当网页上不存在键值时。尽管如此,这似乎是一个非常复杂和混乱的方式来处理这个问题。
如果我删除这些try / exception子句,如果我的刮刀遇到缺少的键,它会返回一个 KeyError
作为KeyError:'username'
,我的脚本停止运行。
任何关于更智能和改进的建议方法来处理这些错误,以便在遇到缺少的密钥时,脚本继续。
我已经尝试创建了一个JSON值的列表,并希望通过一个IF子句来遍历它们,但是我无法理解。
使用 .get()
方法:
> ;>> a = {'bar':'eggs'}
>>>一个['foo']
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
KeyError:'foo'
>>> a.get('foo','default value')
'默认值'
>>> a.get('bar','default value')
'eggs'
.get()
方法返回所请求密钥的值,如果密钥缺失,则返回默认值。
或者,您可以为每个关键字创建一个带有空字符串的新dict,并使用 .update()
:
profile = dict.fromkeys('id name first_name last_name link username gender locale'.split(),'')
profile.update(result)
dict.fromkeys()
创建一个字典,其中所有要求的键设置为给定的默认值(''
在上面的例子中),然后我们使用 .update()
从结果
字典,替换任何已经在那里。
I currently have a Python 2.7 script which scrapes Facebook and captures some JSON data from each page. The JSON data contains personal information. A sample of the JSON data is below:-
{
"id": "4",
"name": "Mark Zuckerberg",
"first_name": "Mark",
"last_name": "Zuckerberg",
"link": "http://www.facebook.com/zuck",
"username": "zuck",
"gender": "male",
"locale": "en_US"
}
The JSON values can vary from page to page. The above example lists all the possibles but sometimes, a value such as 'username' may not exist and I may encounter JSON data such as:-
{
"id": "6",
"name": "Billy Smith",
"first_name": "Billy",
"last_name": "Smith",
"gender": "male",
"locale": "en_US"
}
With this data, I want to populate a database table. As such, my code is as below:-
results_json = simplejson.loads(scraperwiki.scrape(profile_url))
for result in results_json:
profile = dict()
try:
profile['id'] = int(results_json['id'])
except:
profile['id'] = ""
try:
profile['name'] = results_json['name']
except:
profile['name'] = ""
try:
profile['first_name'] = results_json['first_name']
except:
profile['first_name'] = ""
try:
profile['last_name'] = results_json['last_name']
except:
profile['last_name'] = ""
try:
profile['link'] = results_json['link']
except:
profile['link'] = ""
try:
profile['username'] = results_json['username']
except:
profile['username'] = ""
try:
profile['gender'] = results_json['gender']
except:
profile['gender'] = ""
try:
profile['locale'] = results_json['locale']
except:
profile['locale'] = ""
The reason I have so many try/excepts is to account for when the key value doesn't exist on the webpage. Nonetheless, this seems to be a really clumpsy and messy way to handle this issue.
If I remove these try / exception clauses, should my scraper encounter a missing key, it returns a KeyError
such as "KeyError: 'username'"
and my script stops running.
Any suggestions on a much smarter and improved way to handle these errors so that, should a missing key be encountered, the script continues.
I've tried creating a list of the JSON values and looked to iterate through them with an IF clause but I just can't figure it out.
Use the .get()
method instead:
>>> a = {'bar': 'eggs'}
>>> a['foo']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'foo'
>>> a.get('foo', 'default value')
'default value'
>>> a.get('bar', 'default value')
'eggs'
The .get()
method returns the value for the requested key, or the default value if the key is missing.
Or you can create a new dict with empty strings for each key and use .update()
on it:
profile = dict.fromkeys('id name first_name last_name link username gender locale'.split(), '')
profile.update(result)
dict.fromkeys()
creates a dictionary with all keys you request set to a given default value (''
in the above example), then we use .update()
to copy all keys and values from the result
dictionary, replacing anything already there.
这篇关于使用JSON和字典处理Python中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!