IndexError:列表索引超出范围本体注释

问题描述

我正在使用以下代码在本体中添加注释

from owlready import *
onto = get_ontology("C://Users//sharm//Documents//ISWC2020//Ontology_read_play//Covid_v1.owl")
ANNOTATIONS[Thing].add_annotation("comment","My comment")
onto.save()

当我使用on.save()保存它时,它会显示一个IndexError

IndexError Traceback (most recent call last)<module>
--> onto.save()

C:\ProgramData\Anaconda3\lib\site-packages\owlready\__init__.py in save(self,filename)
282     owl = to_owl(self)
283     if filename: f = open(filename,"w")
--> 284     else:        f = _open_onto_file(self.base_iri,self.name,"w")
285     print("* Owlready * Saving ontology %s to %s..." % (self.name,getattr(f,"name","???")),file = sys.stderr)
286     f.write(owl)

C:\ProgramData\Anaconda3\lib\site-packages\owlready\__init__.py in _open_onto_file(base_iri,name,mode,only_local)
199     if os.path.exists(filename): return open(filename,mode)
200   if (mode == "r") and not only_local: return urllib.request.urlopen(base_iri)
--> 201   if (mode == "w"): return open(os.path.join(onto_path[0],"%s.owl" % name),"w")
202   raise FileNotFoundError
203 

IndexError: list index out of range

解决方法

对文档(enter image description here)的快速搜索显示,如果您未在companies[0].salesReps[i].name 方法中指定文件或文件名,它将使用save模块变量中的第一个路径。您从来没有设置它,显然onto_path开始为空。

onto_path方法中添加适当的名称参数,并在下次遇到问题时签出文档。