Python中的集合项

2024-05-15 15:09:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的mongoDB数据库中,我有一个如下项目的集合:

{u'Keywords': [[u'european', 7], [u'bill', 5], [u'uk', 5], [u'years', 4], [u'brexit', 4]], u'Link': u'http://www.bbc.com/
news/uk-politics-39042876', u'date': datetime.datetime(2017, 2, 21, 22, 47, 7, 463000), u'_id': ObjectId('58acc36b3040a218bc62c6d3')}
.....

它们来自mongo数据库查询

   mydb = client['BBCArticles']
    ##mydb.adminCommand({'setParameter': True, 'textSearchEnabled': True})
    my_collection = mydb['Articles']
    print 'Articles containing  higher occurences of the keyword is sorted as follow:'
    for doc in my_collection.find({"Keywords":{"$elemMatch" : {"$elemMatch": {"$in": [keyword.lower()]}}}}):
        print doc

但是,我想按如下方式打印文档:

doc1
Kewords: european,bill, uk
Link:"http://www.bbc.com/"

doc2
....

Tags: com数据库truehttpdatetimemywwwlink
1条回答
网友
1楼 · 发布于 2024-05-15 15:09:18

因为您的集合看起来像是list个字典,所以应该可以使用for循环进行iterable和parseable。如果您确实只需要url和关键字的一部分,这应该可以:

# c = your_collection, a list of dictionaries

from urlparse import urlparse

for n in range(len(c)):
    print 'doc{n}'.format(n=n+1)
    for k, v in c[n].iteritems():
        if k == 'Keywords':
            print k+':', ', '.join([str(kw[0]) for kw in v[0:3]])
        if k == 'Link':
            parsed_uri = urlparse( v )
            domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
            print k+':', '"{0}"\n'.format(domain)

印刷品:

doc1
Keywords: european, bill, uk
Link: "http://www.bbc.com/"

相关问题 更多 >