使用 NetworkX 读取 GraphML 文件时的 KeyError 异常发生在 Visual Studio 2017 中,但不是 Spyder

问题描述

我正在尝试使用 Visual Studio 2017 社区中的 NetworkX Python 包读取 GraphML 文件,使用以下代码

import networkx as nx
import glob
import re

data = {}
graphs = {}

for graphml in glob.glob("*.graphml"):
    m = re.match(r'^ant_mersch_col([0-9]+)_day([0-9]+)_attribute.graphml$',graphml).groups()

    if m[0] not in data.keys():
        data[m[0]] = []
        graphs[m[0]] = {}

    data[m[0]].append(m[1])

    graphs[m[0]][m[1]] = nx.read_graphml(graphml)

为简洁起见,我省略了大部分节点和边,但这是我正在尝试阅读的缩写 GraphML 文件

<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"><key id="d16" for="edge" attr.name="weight" attr.type="long"/>
<key id="d15" for="node" attr.name="nb_interaction_queen" attr.type="double"/>
<key id="d14" for="node" attr.name="nb_interaction_foragers" attr.type="double"/>
<key id="d13" for="node" attr.name="nb_interaction_cleaners" attr.type="double"/>
<key id="d12" for="node" attr.name="nb_interaction_nurses" attr.type="double"/>
<key id="d11" for="node" attr.name="visits_to_rubbishpile" attr.type="double"/>
<key id="d10" for="node" attr.name="visits_to_nest_entrance" attr.type="double"/>
<key id="d9" for="node" attr.name="visits_to_brood" attr.type="double"/>
<key id="d8" for="node" attr.name="group_period4" attr.type="string"/>
<key id="d7" for="node" attr.name="group_period3" attr.type="string"/>
<key id="d6" for="node" attr.name="group_period2" attr.type="string"/>
<key id="d5" for="node" attr.name="group_period1" attr.type="string"/>
<key id="d4" for="node" attr.name="nb_foraging_events" attr.type="long"/>
<key id="d3" for="node" attr.name="age(days)" attr.type="long"/>
<key id="d2" for="node" attr.name="body_size" attr.type="double"/>
<key id="d1" for="node" attr.name="tag_id" attr.type="long"/>
<key id="d0" for="node" attr.name="colony" attr.type="long"/>
<graph edgedefault="undirected">
<node id="Ant620">
  <data key="d0">4</data>
  <data key="d1">620</data>
  <data key="d2">141.45</data>
  <data key="d3">57</data>
  <data key="d4">0</data>
  <data key="d5">N</data>
  <data key="d6">C</data>
  <data key="d7"> </data>
  <data key="d8"> </data>
  <data key="d9">137.909</data>
  <data key="d10">0.909091</data>
  <data key="d11">4.63636</data>
  <data key="d12">161.63636</data>
  <data key="d13">169.36364</data>
  <data key="d14">68.363636</data>
  <data key="d15">8.636000000000001</data>
</node>
<edge source="Ant347" target="Ant186">
  <data key="d16">2</data>
</edge>
</graph>
</graphml>

顺便说一下,这个文件是来自 Network Repository 的 ant 数据;对于那些好奇的人,可以找到完整的文件here

我在我的 IDE 中得到了一个相当模糊的异常 'long',它显然是一个基于堆栈跟踪的 KeyError

Traceback (most recent call last):
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py",line 119,in <module>
    
vspd.debug(filename,port_num,debug_id,debug_options,run_as)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\debugger.py",line 37,in debug
    
run(address,filename,*args,**kwargs)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_local.py",line 64,in run_file
    
run(argv,addr,line 125,in _run
    
_pydevd.main()

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py",line 1752,in main
    
globals = debugger.run(setup['file'],None,is_module)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py",line 1099,in run
    
return self._exec(is_module,entry_point_fn,module_name,file,globals,locals)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py",line 1106,in _exec
    
pydev_imports.execfile(file,locals)  # execute the script

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py",line 25,in execfile
    
exec(compile(contents+"\n",'exec'),glob,loc)

  File "C:\Users\Ethan_000\Source\Repos\Research\Research\Ants.py",line 22,in <module>
    
nx.read_graphml(graphml)

  File "<decorator-gen-692>",line 2,in read_graphml
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\site-packages\networkx\utils\decorators.py",line 239,in _open_file
    
result = func_to_be_decorated(*new_args,**kwargs)

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\site-packages\networkx\readwrite\graphml.py",line 258,in read_graphml
    
glist = list(reader(path=path))

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\site-packages\networkx\readwrite\graphml.py",line 775,in __call__
    
(keys,defaults) = self.find_graphml_keys(self.xml)

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\site-packages\networkx\readwrite\graphml.py",line 951,in find_graphml_keys
    
"type": self.python_type[attr_type],KeyError
: 
'long'

奇怪的是,当我在 Spyder 中运行几乎相同的代码时,不会发生这种情况。

有谁知道为什么这会在 Visual Studio 2017 中发生(但在 Spyder 中不会)以及我该如何解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...