我正在使用pymongo连接到我的mongodb:
client = MongoClient()
mongo = MongoClient('localhost', 27017)
mongo_db = mongo['test']
mongo_coll = mongo_db['test'] #Tweets database
我有一个游标,正在遍历每条记录:
cursor = mongo_coll.find()
for record in cursor: #for all the tweets in the database
try:
msgurl = record["entities"]["urls"] #look for URLs in the tweets
except:
continue
的原因try/except
是因为如果["entities"]["urls"]
不存在,它就会出错。
如何判断是否["entities"]["urls"]
存在?