0

所以这可能是一个非常简单的问题。

我正在尝试访问评论家评论中名为 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]

如何访问 original_score 并将 null 分配给没有任何评论的评论?

谢谢!

4

1 回答 1

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]
于 2014-10-25T05:08:43.853 回答