2016-08-17 65 views
1

这是一个函数,它返回一个错误,与post['date']一致。Python函数pymongo中的错误

这是错误:

in get_posts 
    post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p") 
KeyError: 'date' 

是什么意思?

这是函数:

def get_posts(self, num_posts): 

    cursor = self.posts.find({},{}).limit(num_posts) # Using an empty itable for a placeholder so blog compiles before you make your changes 
    # XXX HW 3.2 Work here to get the posts 
    l = [] 

    for post in cursor: 
     print post   
     post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p") 
     if 'tags' not in post: 
      post['tags'] = [] # fill it in if its not there already 
     if 'comments' not in post: 
      post['comments'] = [] 

     l.append({'title':post['title'], 'body':post['body'], 'post_date':post['date'], 
        'permalink':post['permalink'], 
        'tags':post['tags'], 
        'author':post['author'], 
        'comments':post['comments']}) 

    return l 
+0

可能重复的[我得到重要错误在蟒蛇](http://stackoverflow.com/questions/10116518/im-getting-key-error-in-python) – fzzle

回答

0

KeyError: 'date'意味着post没有属性date