所以这可能是一个非常简单的问题。
我正在尝试在评论家评论中访问一个名为original_score的字段,而这个字段在某些内容中可用,而在其他字段中则不可用。
{u'publication': u'Village Voice', u'links': u'quote': u'When teenaged Andy plops down on the grass to share his old toys with a shy little girl, the film spikes with sadness and layered pleasure -- a concise, deeply wise expression of the ephemeral that feels real and yet utterly transporting.', u'freshness': u'fresh', u'critic': u'Eric Hynes', u'date': u'2013-08-04'}
{u'publication': u'New Yorker', u'links': u'quote': u'There are many sweet laughs and joking allusions to horror and prison-break movies, but the Pixar gang gets at the most primary fear -- being cast off and no longer of use.', u'freshness': u'fresh', u'critic': u'David Denby', u'date': u'2013-08-04'}
{u'publication': u'CNN.com', u'links': { u'quote': u'I seriously doubt there will be a more hilarious and heartfelt blockbuster all summer.', u'freshness': u'fresh', u'critic': u'Tom Charity', u'date': u'2013-08-04'}
{u'publication': u'TheWrap', u'links': { u'quote': u"It's still more inventive, clever and laugh-out-loud funny than any other movie out there now.", u'freshness': u'fresh', u'critic': u'Leah Rozen', u'date': u'2011-10-07'}
{u'publication': u'Time Out', u'links': {u'quote': u"The 'Toy Story' films are deservedly seen as the gold standard for computer-generated animation...", u'freshness': u'fresh', u'original_score': u'4/5', u'critic': u'Ben Walters', u'date': u'2010-07-15'}
对于所有评论中显示的项目,我可以使用以下方式访问它们:
data = data['reviews']
data = [dict(fresh=r['freshness'],
quote=r['quote'],
critic=r['critic'],
publication=r['publication'],
review_date=r['date'],
imdb=imdb, rtid=rtid
)
for r in data]
如何访问originial_score并为没有任何评论的评论指定null?
谢谢!
答案 0 :(得分:1)
使用三元表达式为original_score分配值:
original_score = r['original_score'] if 'original_score' in r.keys() else None
就是这样:
data = [dict(fresh=r['freshness'],
quote=r['quote'],
critic=r['critic'],
publication=r['publication'],
review_date=r['date'],
imdb=imdb, rtid=rtid,
original_score = r['original_score'] if 'original_score' in r.keys() else None)
for r in data]