2017-08-24 73 views
-1

這是代碼:蟒蛇pymongo找到KeyError異常

select_sql = 'select url from baidu_v_tbl' 
results = cur_oracle.execute(select_sql) 
`enter code here`urls = results.fetchall() 

    logging.info('start........... ') 

    for con in urls: 
     url = con[0] 
     url_v = ''.join(['https://www.baidu.com/s?wd=', url, '@v'])  mongo_records = db_mongo.keywords.find({'url': url_v},{'url': 1, 'keyword': 1}, no_cursor_timeout=True) 

    mongo_keywords = [key['keyword'] for key in mongo_records if key['keyword']] 

這是錯誤:

Traceback (most recent call last): 
    File "from_keywords_into_oracle.py", line 44, in <module> 
    mongo_keywords = [key['keyword'] for key in mongo_records] 
KeyError: 'keyword' 
+0

你能縮進/格式化代碼:

您可以通過更換? –

回答

0

唯一的例外是明確的: 「KeyError異常 '關鍵字'」。

變量mongo_recordsdict對象(或類似字典)的集合,並且您至少得到一個沒有關鍵字'keyword'的項。

mongo_keywords = [key['keyword'] for key in mongo_records if 'keyword' in key] 
+0

你,thx爲你的幫助,它的工作, – blackang

+0

,但現在一個新的問題 – blackang

+0

@blackang請在SO上發佈一個新的問題。 –