简体   繁体   中英

How to read a GraphML file using networkx in Python?

I'm new to networkx and Neo4j. I have imported a graph database to Neo4j using the 'Import method' because it was a huge graph and it couldn't be loaded. Now, I want to do some graph analytics using networkx. So, as I understood that I should do the following steps:

  1. Export the graph as GraphML format from Neo4j.
  2. Read the GraphML file using networkx in Python.

When reading the GraphML file I face this error: Bad GraphML data: no key labels. I opened the GraphML file in notepad and I guess Neo4j is creating a wrong GraphML file. I just have one node but it's considering two key labels for nodes.

below is a snippet of the GraphML file. Can anyone help me out with this?

:位置点(-87.9030396611 41.9790708201)

This seems to have been an issue for a while: https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/478 .

What networkx complains about is that a node has data that has the key "labels," but there is no key with id "labels" defined. Thereby the exception.

To avoid this, you have (at least) two options. You could do as suggested in the issue I linked, paste this to the top section of the graphML file:

<key id="labels" for="node" attr.name="labels"/>

Another option is to set the format to "gephi" in the configuration (shown below). When doing so, the data tag that referenced the "labels" is instead referencing the "TYPE" key, which is defined in the key-tags.

call apoc.export.graphml.all(<your-path>, {format:"gephi"})

Also, with the default Neo4j export, networkx generates a ton of warnings, which you can remove by setting the useTypes to true.

call apoc.export.graphml.all(<your-path>, {format:"gephi", useTypes:true})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM