繁体   English   中英

没有找到 rdflib.plugins.sparql?

[英]rdflib.plugins.sparql not found?

我正在用 python 研究一些基本的 rdflib 东西,试图弄清楚,但我遇到了一个看似基本的问题。

代码也在这里,第三课下的第三个例子:SPARQL。

import rdflib

g = rdflib.Graph()
g.parse("family.ttl", format='ttl')

q = rdflib.plugins.sparql.prepareQuery(
        """SELECT ?child ?sister WHERE {
                  ?child fam:hasParent ?parent .
                  ?parent fam:hasSister ?sister .
       }""",
       initNs = { "fam": "http://example.org/family#"})

sm = rdflib.URIRef("http://example.org/royal#SverreMagnus")

for row in g.query(q, initBindings={'child': sm}):
        print(row)

family.ttl 文件在同一个目录下,按照示例如下。

@prefix rex: <http://example.org/royal#> .
@prefix fam: <http://example.org/family#> .

rex:IngridAlexandra fam:hasParent rex:HaakonMagnus .
rex:SverreMagnus fam:hasParent rex:HaakonMagnus .
rex:HaakonMagnus fam:hasParent rex:Harald .
rex:MarthaLouise fam:hasParent rex:Harald .
rex:HaakonMagnus fam:hasSister rex:MarthaLouise .

当我尝试运行它时,出现错误。

AttributeError: module 'rdflib.plugins' has no attribute 'sparql'

Python 是 3.9.5 版本,rdflib 是 6.0.1。 有谁知道交易可能是什么?

只需尝试稍微更改您的导入,如下所示:

import rdflib
from rdflib.plugins import sparql

g = rdflib.Graph()
g.parse("family.ttl")

q = sparql.prepareQuery(
    """SELECT ?child ?sister WHERE {
              ?child fam:hasParent ?parent .
              ?parent fam:hasSister ?sister .
   }""",
   initNs={"fam": "http://example.org/family#"})

sm = rdflib.URIRef("http://example.org/royal#SverreMagnus")

for row in g.query(q, initBindings={'child': sm}):
    print(row)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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