简体   繁体   English

如何使用SPARQL查询ttl文件?

[英]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 - 所以两个问题-

  1. Why is it doing this if my version of rdflib >=4 ? 如果我的rdflib >=4版本rdflib >=4rdflib >=4
  2. Ultimately - this is all beside the point. 最终-这一切都不重要。 I don't care about the means, so how do I use python to run SPARQL queries against local ttl files (in a way that doesn't require admin rights on my computer) 我不在乎这种方法,所以我如何使用python对本地ttl文件运行SPARQL查询(这种方式不需要我的计算机上的管理员权限)

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.

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