SPARQLWrapper: how to interpolate python variables in query

542 views
Skip to first unread message

Robert Alexander

unread,
Jan 7, 2022, 2:17:48 PM1/7/22
to rdflib-dev
Dear friends,
I am a beginner so please bear with me.
I find the """ """ style of writing the queries in my code very readable but I am not able to introduce a variable in it.
For example:
sparql.setQuery("""
select distinct ?id ?eurovoc
where {
?atto a ocd:aic .
?atto dc:identifier ?id .
?atto ocd:startDate ?datapres .
FILTER(REGEX(?datapres,'2021\\\\d{4}'))
}
""")

in the above query I would like 2021 to be instantiated by a "year" variable I previously set in the code.

Any suggestions? Thank you very much and happy 2022
Robert

Marijane White

unread,
Jan 7, 2022, 2:35:42 PM1/7/22
to rdfli...@googlegroups.com

Hello Robert,

 

You’ll need to use one of Python’s string formatting features, which provide functionality for inserting variables into strings. The current state of the art are f-strings, but note that you will have to double all the curly braces in your query string if you use them because f-strings use curly braces to denote variable names.

 

Here’s a nice article from Real Python about all three ways to format strings in Python, and their various pros and cons.

https://realpython.com/python-f-strings/

 

 

Marijane White, M.S.L.I.S.

Data and Research Engagement Librarian, Assistant Professor

Oregon Health & Science University Library

 

Email: whi...@ohsu.edu

ORCiD: https://orcid.org/0000-0001-5059-4132

--
http://github.com/RDFLib
---
You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/e6752549-3d70-413d-9844-338a76c2cfd2n%40googlegroups.com.

Nicholas Car

unread,
Jan 8, 2022, 2:56:55 AM1/8/22
to rdfli...@googlegroups.com
Hi Robert,

Marijane is correct! My favorite way of using variables in RDFlib SPARQL queries is to use string replace() so that I don't have to double-up {  and }, so I would do something like...

...I'll put a complete answer in if you re-post this to Stack Overflow please! That's where we like to keep all our RDFlib How-To questions! This list's just for RDFlib development.

Cheers,

Nick



Gunnar Aastrand Grimnes

unread,
Jan 8, 2022, 3:22:54 AM1/8/22
to rdfli...@googlegroups.com
Using string replace isn't a great idea if you don't trust the input - re: SQL/SPARQL injection and little bobby tables: https://xkcd.com/327/

I don't think it works with SPARQLWrapper - but with the SPARQLStore you can pass your binding as RDFLib terms: 

  dbo = Namespace("http://dbpedia.org/ontology/")
  graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
  graph.open("http://dbpedia.org/sparql")
  graph.bind('dbo', dbo)
  for row in graph.query('SELECT ?pop WHERE { ?city dbo:populationTotal ?pop }', initBindings={ 'city': URIRef("http://dbpedia.org/resource/Brisbane") })):
   .... do something with row ...

For mystery reasons this doesn't work with dbpedia though - it should generate the query: 
SELECT ?pop WHERE { ?city dbo:populationTotal ?pop } VALUES (?city) { ( <http://dbpedia.org/resource/Brisbane>) }

which should work but doesn't. I don't have time to figure that out now though :D 

- Gunnar



--

Robert Alexander

unread,
Jan 8, 2022, 7:57:48 AM1/8/22
to rdfli...@googlegroups.com
Thank you all so much. I will ponder on all good advice you kindly offered. Will continue on Stackoverflow. I routinely use f strings but was dumbfounded by the concurrent use of the triple quotes :) Being a medical docyor I’m only an amateur 😀. Happy and safe 2022 to you all. Robert

Sent from my iPhone

On 8 Jan 2022, at 09:22, Gunnar Aastrand Grimnes <grom...@gmail.com> wrote:


Reply all
Reply to author
Forward
0 new messages