@hal.littel
You can pass python variables to a SPARQL query by using string formatting or interpolation. Here's an example using string formatting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import rdflib
# Create a graph
g = rdflib.Graph()
# Define the namespace
ns = rdflib.Namespace("http://example.org/")
# Define the variable in Python
name = "John"
# Format the SPARQL query with the variable
query = """
SELECT ?person
WHERE {{
?person a {ns}Person .
?person {ns}name "{name}" .
}}
""".format(ns=ns, name=name)
# Execute the query
results = g.query(query)
for row in results:
print(row)
|
In this example, the variable name is passed to the SPARQL query using string formatting. You can replace {name} in the query string with the actual value of the variable.