sparql_query_example
SPARQL Query using Graph.query
The method returns a Result, iterating over
this yields ResultRow objects
The variable bindings can be accessed as attributes of the row objects
For variable names that are not valid python identifiers, dict access
(i.e. with row[var] / __getitem__) is also possible.
Result.vars contains the variables
Attributes:
-
EXAMPLES_DIR– -
g–
alse show_docstring_attributes: false show_docstring_functions: false show_docstring_modules: false show_docstring_classes: false show_signature: false show_signature_annotations: false show_signature_type_parameters: false show_docstring_other_parameters: false show_docstring_parameters: false show_docstring_raises: false show_docstring_receives: false show_docstring_returns: false summary: false show_if_no_docstring: false
import logging
import sys
from pathlib import Path
import rdflib
EXAMPLES_DIR = Path(__file__).parent
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)
g = rdflib.Graph()
g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}", format="n3")
# The QueryProcessor knows the FOAF prefix from the graph
# which in turn knows it from reading the N3 RDF file
for row in g.query("SELECT ?s WHERE { [] foaf:knows ?s .}"):
# For select queries, the Result object is an iterable of ResultRow
# objects.
assert isinstance(row, rdflib.query.ResultRow)
print(row.s)
# or row["s"]
# or row[rdflib.Variable("s")]