在Python中的图形节点对象的dict中添加新键时出错

时间:2014-12-01 15:52:07

标签: python neo4j

我正在玩neo4j数据库,当我想向node属性添加一个列表时,我遇到了困难。 基本上我要添加一个新条目" 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

那我该怎么办?

1 个答案:

答案 0 :(得分:3)

我已经能够重新创建此问题并且刚刚提交修复错误/无用的错误消息。这将在py2neo 2.0.1中发布。

根本问题实际上是非法财产类型的分配。您正在尝试设置由PropertySet对象列表组成的属性值。 Neo4j中的列表可能只包含原始类型,它们必须是同质的。有关详细信息,请查看此处:http://neo4j.com/docs/stable/graphdb-neo4j-properties.html