python - 在Python中的图节点对象的字典中添加新键时出错

标签 python neo4j

我正在使用 neo4j 数据库,当我想向节点属性添加列表时,我陷入了困境。 基本上我想向节点属性添加一个新条目“links_in”,该节点属性是该节点的传入节点属性列表

以下是节点:

   | n                                                                       
---+--------------------------------------------------------------------------
 1 | (n4096:Site {name:"http://www.example.org/"})             
 2 | (n4097:Site {name:"http://www.example.org/our-services"})             
 3 | (n4098:Site {name:"http://www.example.org/our-services#solution"})    
 4 | (n4099:Site {name:"http://www.example.org/our-services#consult"})     
 5 | (n4100:Site {name:"http://www.example.org/our-services#technologies"})
 6 | (n4101:Site {name:"http://www.example.org/about"})                    
 7 | (n4102:Site {name:"http://www.example.org/careers"})                  
 8 | (n4103:Site {name:"http://www.example.org/blog"})                     
 9 | (n4104:Site {name:"http://www.example.org/contact"})

节点关系如下:

"http://www.example.org/" -> "http://www.example.org/our-services"
"http://www.example.org/" -> "http://www.example.org/our-services#solution"
"http://www.example.org/" -> "http://www.example.org/our-services#consult"
"http://www.example.org/" -> "http://www.example.org/our-services#technologies"
"http://www.example.org/" -> "http://www.example.org/about"
"http://www.example.org/" -> "http://www.example.org/careers"
"http://www.example.org/" -> "http://www.example.org/blog"
"http://www.example.org/" -> "http://www.example.org/cont"

我期望输出为:

   | n                                                                       
---+--------------------------------------------------------------------------
 1 | (n4096:Site {name:"http://www.example.org/","links_in":[]})             
 2 | (n4097:Site {name:"http://www.example.org/our-services","links_in":["http://www.example.org/"]})             
 3 | (n4098:Site {name:"http://www.example.org/our-services#solution,"links_in":["http://www.example.org/"]"})    
 4 | (n4099:Site {name:"http://www.example.org/our-services#consult,"links_in":["http://www.example.org/"]"})     
 5 | (n4100:Site {name:"http://www.example.org/our-services#technologies,"links_in":["http://www.example.org/"]"})
 6 | (n4101:Site {name:"http://www.example.org/about,"links_in":["http://www.example.org/"]"})                    
 7 | (n4102:Site {name:"http://www.example.org/careers,"links_in":["http://www.example.org/"]"})                  
 8 | (n4103:Site {name:"http://www.example.org/blog,"links_in":["http://www.example.org/"]"})                     
 9 | (n4104:Site {name:"http://www.example.org/contact,"links_in":["http://www.example.org/"]"})

所以我尝试了以下方法来得到上面的结果:

def FindLinks():
    links_in = []
    for i in graph.cypher.execute("match n return n"):
        for j in graph.cypher.execute("match ()-[r:ok]->(n) where n.name ='%s' return r"%i.n.properties['name']):
            if graph.node(i.n.ref).properties.has_key('links_in'): 
                graph.node(i.n.ref).properties["links_in"].append(j.r.start_node.properties)
            else:
                links_in.append(j.r.start_node.properties)
        graph.node(i.n.ref).properties.update({"links_in":links_in})
        links_in = []

但是我得到了错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-171-e762c2030688> in <module>()
      5         else:
      6             links_in.append(j.r.start_node.properties)
----> 7     graph.node(i.n.ref).properties.update({"links_in":links_in})
      8     links_in = []

/usr/local/lib/python2.7/dist-packages/py2neo/core.pyc in update(self, iterable, **kwargs)
   1071                     self[key] = iterable[key]
   1072             except (AttributeError, TypeError):
-> 1073                 for key, value in iterable:
   1074                     self[key] = value
   1075         for key in kwargs:

ValueError: too many values to unpack

那我该怎么办?

最佳答案

我已经能够重现此问题,并且刚刚做出 promise 来修复不正确/无用的错误消息。这将在 py2neo 2.0.1 中发布。

根本问题实际上是非法属性类型的分配。您正在尝试设置由 PropertySet 对象列表组成的属性值。 Neo4j 中的列表只能包含原始类型,并且它们必须是同质的。欲了解更多信息,请查看这里:http://neo4j.com/docs/stable/graphdb-neo4j-properties.html .

关于python - 在Python中的图节点对象的字典中添加新键时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27232082/

相关文章:

python - Pandas Python 中的 SUMIFS 公式

neo4j - Neo4j中MATCH的"NOT"运算符

Neo4j:自定义路径遍历

python - pycharm 中的 Jupyter notebook 不显示内联图

python - 如何从同级文件夹中导入 Python 模块?

用于验证字符串的python re

neo4j - neo4j 图数据库中的元数据

json - 如何将 Neo4j 导出的 JSON 文件导入 D3

neo4j - 如何将查询语句记录到 Neo4j 服务器,这可能吗?

python - 在 Python 中使用 ffmpeg 即时将 mp3 转换为 wav