Dano13
Dano13

Reputation: 159

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   "[email protected]" . 

ab:cindy ab:homeTel "(245) 646-5488" . 
ab:cindy ab:email   "[email protected]" . 

ab:craig ab:homeTel "(194) 966-1505" . 
ab:craig ab:email   "[email protected]" . 
ab:craig ab:email   "[email protected]" .

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.

Upvotes: 3

Views: 2772

Answers (1)

Dano13
Dano13

Reputation: 159

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

Upvotes: 1

Related Questions