我正在研究python中的一些基本的rdflib内容,试图解决它,并且遇到了一个看似基本的问题。
代码也是这里,这是第3课下的第三个例子: 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。有人知道交易可能是什么吗?
发布于 2021-12-17 06:31:27
只需尝试更改您的导入,如下所示:
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)https://stackoverflow.com/questions/69984830
复制相似问题