Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyMongo—`If`不';在游标中不能正常工作_Python_Mongodb_Cursor_Pymongo - Fatal编程技术网

Python PyMongo—`If`不';在游标中不能正常工作

Python PyMongo—`If`不';在游标中不能正常工作,python,mongodb,cursor,pymongo,Python,Mongodb,Cursor,Pymongo,我有一个MongoDB,文件如下: 我有一个txt文件,其中包括一些单词和它们的情绪分数 我想在MongoDB中找到这些单词,但有一定的关系,我想在新集合中插入这些文档的字段 代码: for w in words: print w cursor = db.collectionName.find({ 'surfaceStart': w }) for document in cursor: relation =

我有一个MongoDB,文件如下:

我有一个txt文件,其中包括一些单词和它们的情绪分数

我想在MongoDB中找到这些单词,但有一定的关系,我想在新集合中插入这些文档的字段

代码:

for w in words:
            print w
            cursor = db.collectionName.find({ 'surfaceStart': w })
        for document in cursor:
            relation = document['rel']
            word = document['surfaceEnd'].encode('utf-8')
            posnum = float(get_positive(cols))
            negnum = float(get_negative(cols))
            if (document['rel']).find('Synonym'):
                db.testcollection.insert ({ 'surfaceStart': w,'surfaceEnd': word,'Relation': relation, "pos": posnum, "neg": negnum })

            if (document['rel']).find('Antonym'):

                db.testcollection.insert ({ 'surfaceStart': w,'surfaceEnd': word,'Relation': relation, "pos": posnum, "neg": negnum })
            if (document['rel']).find('Related') or (document['rel']).find('Derived'):
                db.testcollection.insert ({ 'surfaceStart': w,'surfaceEnd': word,'Relation': relation, "pos": posnum, "neg": negnum })
不幸的是,这段代码有一个奇怪的行为。 似乎没有对关系的控制,在
testcollection
中为每个关系插入文档3次。
我不明白为什么会发生这种情况,因为我有IF函数。

document['rel']
在您发布的所有示例中似乎都是一个字符串:“r/instanceOf”。字符串上的
find
方法返回项目在字符串中的位置,如果未找到,则返回-1-1在布尔上下文中为真

无论如何,您都不关心字符串的位置,因此不应该使用
find
。在中使用普通的

if 'Synonym' in document['rel']:
    ...

if 'Related' in document['rel'] or 'Derived' in document['rel']: