简体   繁体   中英

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. I have researched this and every response says to use rdflib and then run a query against that.

Here is the example ttl file I am using

# 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

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'

Okay - no problem. So I go to conda-forge to install it, and it says that package is not available from current channels.

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 . So I check my version of rdflib and it is 4.2.2 . So obviously something is wrong, since it shouldn't require rdfextras .

So two questions -

  1. Why is it doing this if my version of rdflib >=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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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