我正在使用查询从mongodb获取嵌套记录
@classmethod
def fetch_links(self, web_id):
cursor = self.db.websites.find({"_id": ObjectId(web_id)}, {"links": 1})
results = list(cursor)
return results我将这个函数调用为:
old_links = Signatures.fetch_links(web_id)
print(old_links)和gettin结果为:
[{
'_id': ObjectId('5ac5efd6a37efa4c0e28f5aa'),
'links': [{
'type': 'np',
'link_id': 'quotes-1',
'link': '/'
}, {
'type': 'np',
'link_id': 'quotes-2',
'link': '/login'
}, {
'type': 'np',
'link_id': 'redcarpetsupport-1',
'link': 'AR/index.html'
}, {
'type': 'np',
'link_id': 'redcarpetsupport-3',
'link': 'services.html'
}]}]
现在我想像这样访问:
print(old_links['link']), old_links(links['link_id']) and print(old_links['type'])感谢您的帮助。提前谢谢。
发布于 2018-04-16 19:56:44
我通过将list转换为数组并在for中循环两次修复了这个问题。我不知道它是好是坏。
import numpy as np
old_links = Signatures.fetch_level1_links(web_id)
link_arr = np.array(old_links)
for item in link_arr:
#print(item['links'])
for link in item['links']:
print(link['type'])https://stackoverflow.com/questions/49854155
复制相似问题