TypeError: 'NoneType' object does not support item assignment

1.2k Views Asked by At

I am trying to parse a GraphMl file with the following Code

And i am getting the following error.

Would you please help me in solving this?

1

There are 1 best solutions below

0
On

Note: it's the first time that I encounter pygraphml.

The problem seems to be a logical error in your graph definition file (test.graphml). On top of that pygraphml2.0, doesn't seem to handle well such errors:

As pointed out, the error: TypeError: 'NoneType' object does not support item assignment, comes from graphml_parser.py line 114:

e[attr.getAttribute("key")] = attr.firstChild.data

e is/should be an object of type pygraphml.edge.Edge, and is initialized in the same file at line 110:

e = g.add_edge_by_label(source, dest)

the problem is that e is None, and hence the error. g (that instantiates e) is an object of type pygraphml.graph.Graph. Going to graph.py, add_edge_by_label is defined at line 147:

def add_edge_by_label(self, label1, label2):
    """
    """

    n1 = None
    n2 = None

    for n in self._nodes:
        if n['label'] == label1:
            n1 = n
        if n['label'] == label2:
            n2 = n

    if n1 and n2:
        return self.add_edge(n1, n2)
    else:
        return

So, you're hitting the last return statement, meaning that in test.graphml you have (at least) one edge that has one label (or maybe both), that doesn't belong to any of the graph's nodes.

Obviously there's a problem (I consider it a bug) with pygraphml, if Graph.add_edge_by_label can return None, the None test should be performed GraphMLParser.parse (possibly elsewhere as well).

HTH (if it doesn't, you could send me the graph file to have a look at it).