首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rdflib.plugins.sparql没找到吗?

rdflib.plugins.sparql没找到吗?
EN

Stack Overflow用户
提问于 2021-11-16 06:49:01
回答 1查看 224关注 0票数 0

我正在研究python中的一些基本的rdflib内容,试图解决它,并且遇到了一个看似基本的问题。

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

代码语言:javascript
复制
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文件位于同一个目录中,如下所示。

代码语言:javascript
复制
@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 .

当我尝试运行这个程序时,我会得到一个错误。

代码语言:javascript
复制
AttributeError: module 'rdflib.plugins' has no attribute 'sparql'

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-17 06:31:27

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69984830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档