0

我正在尝试使用 owlready 库创建的 owl 文件中的 python 查询数据。但我收到以下错误。原因是什么?

代码结构和接收到的错误如下。

from owlready2 import *
from urllib.request import urlopen
from rdflib.graph import Graph

onto = default_world.get_ontology("http://muratkilinc.com/ontologies/izmir.owl").load()

graph = default_world.as_rdflib_graph()
r = list(graph.query_owlready("""
    PREFIX uni:<http://muratkilinc.com/ontologies/izmir.owl>
    SELECT ?adi ?soyadi ?yas
    WHERE
    {
        ?turistler uni:yas ?yas.
        ?turistler uni:adi ?adi.
        ?turistler uni:soyadi ?soyadi.
        FILTER(?yas > 35).

    }"""))

results = default_world.as_rdflib_graph().query_owlready(r)
results = list(results)
print(results)

错误:

* Owlready2 * Warning: optimized Cython parser module 'owlready2_optimized' is not available, 
defaulting to slower Python implementation
Traceback (most recent call last):
  File "c:/Users/BAUM-PC/Desktop/izmir/sparql.py", line 21, in <module>
    results = list(results)
  File "C:\Users\BAUM-PC\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\owlready2\rdflib_store.py", line 261, in query_owlready
  File "C:\Users\BAUM-PC\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\rdflib\graph.py", line 1089, in query
    query_object, initBindings, initNs, **kwargs))
  File "C:\Users\BAUM-PC\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\rdflib\plugins\sparql\processor.py", line 74, in query
    parsetree = parseQuery(strOrQuery)
  File "C:\Users\BAUM-PC\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\rdflib\plugins\sparql\parser.py", line 1057, in parseQuery
    q = expandUnicodeEscapes(q)
  File "C:\Users\BAUM-PC\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\rdflib\plugins\sparql\parser.py", line 1048, in expandUnicodeEscapes
    return expandUnicodeEscapes_re.sub(expand, q)
TypeError: expected string or bytes-like object
4

1 回答 1

1

您必须跳过第二个查询,错误消息将跳过

from owlready2 import *
from rdflib.graph import Graph

onto = default_world.get_ontology("http://muratkilinc.com/ontologies/izmir.owl").load()

graph = default_world.as_rdflib_graph()

r = list(graph.query_owlready("""
    PREFIX uni:<http://muratkilinc.com/ontologies/izmir.owl>
    SELECT ?adi ?soyadi ?yas
    WHERE
    {
        ?turistler uni:yas ?yas.
        ?turistler uni:adi ?adi.
        ?turistler uni:soyadi ?soyadi.
        FILTER(?yas > 35).
    }"""))

print(list(r))

它给出了空列表 - 因此它可以在没有错误消息的情况下工作。

空列表是不同的问题——查询,而不是代码——所以你应该问新问题。

于 2019-12-08T22:33:56.913 回答