[英]How to use SPARQL to query a ttl file?
I am trying to learn SPARQL and to do that I need to execute queries against a local ttl file. 我正在尝试学习SPARQL,为此我需要对本地ttl文件执行查询。 I have researched this and every response says to use rdflib
and then run a query against that. 我已经对此进行了研究,每个响应都说要使用rdflib
,然后rdflib
运行查询。
Here is the example ttl file I am using 这是我正在使用的示例ttl文件
# filename: ex002.ttl
@prefix ab: <http://learningsparql.com/ns/addressbook#> .
ab:richard ab:homeTel "(229) 276-5135" .
ab:richard ab:email "richard49@hotmail.com" .
ab:cindy ab:homeTel "(245) 646-5488" .
ab:cindy ab:email "cindym@gmail.com" .
ab:craig ab:homeTel "(194) 966-1505" .
ab:craig ab:email "craigellis@yahoo.com" .
ab:craig ab:email "c.ellis@usairwaysgroup.com" .
Now the python code that is supposed to work is the following 现在应该正常工作的python代码如下
filename = "C:/DataStuff/SemanticOntology/LearningSPARQLExamples/ex002.ttl"
import rdflib
g = rdflib.Graph()
result = g.parse(filename, format='ttl')
print(result)
query = """
SELECT * WHERE {
?s ?p ?o .
}
"""
g.query(query)
for stmt in g:
print(stmt)
Unfortunately whenever it gets to g.query(query)
I get ModuleNotFoundError: No module named 'rdfextras'
不幸的是,每当它到达g.query(query)
我就会收到ModuleNotFoundError: No module named 'rdfextras'
Okay - no problem. 好的没问题。 So I go to conda-forge to install it, and it says that package is not available from current channels. 因此,我去conda-forge进行安装,它说当前渠道不提供该软件包。
Okay, weird. 好吧,很奇怪 So I do some research and discover https://github.com/RDFLib/rdfextras which says it is discontinued and it is no longer required for rdflib
>=4
. 因此,我进行了一些研究,发现https://github.com/RDFLib/rdfextras ,它说它已停产,并且rdflib
>=4
不再需要它。 So I check my version of rdflib
and it is 4.2.2
. 所以我检查了rdflib
版本,它是4.2.2
。 So obviously something is wrong, since it shouldn't require rdfextras
. 因此很明显有问题,因为它不需要rdfextras
。
So two questions - 所以两个问题-
rdflib >=4
? 如果我的rdflib >=4
版本rdflib >=4
, rdflib >=4
? Thanks in advance. 提前致谢。
I apologize - I just got home and re-ran the query and now it seems to be working. 我很抱歉-我刚到家并重新运行了查询,现在它似乎正在运行。 I have no idea what changed between now and about 2 hours ago 我不知道从现在到大约2小时前发生了什么变化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.