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 <gromg...@gmail.com> wrote: > > > 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 > >> On Sat, 8 Jan 2022 at 08:56, Nicholas Car >> <nicholas....@surroundaustralia.com> wrote: >> 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 >> >> >> >>> On Sat, Jan 8, 2022 at 5:35 AM Marijane White <whi...@ohsu.edu> wrote: >>> 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 >>> >>> >>> >>> >>> >>> From: <rdflib-dev@googlegroups.com> on behalf of Robert Alexander >>> <gogone...@gmail.com> >>> Reply-To: "rdflib-dev@googlegroups.com" <rdflib-dev@googlegroups.com> >>> Date: Friday, January 7, 2022 at 11:17 AM >>> To: rdflib-dev <rdflib-dev@googlegroups.com> >>> Subject: [EXTERNAL] [rdflib-dev] SPARQLWrapper: how to interpolate python >>> variables in query >>> >>> >>> >>> 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 . >>> >>> >>> >>> ?atto <http://purl.org/dc/terms/subject> ?eurovoc . >>> >>> >>> >>> 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 >>> >>> >>> >>> -- >>> 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+unsubscr...@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. >>> >>> >>> -- >>> 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+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/rdflib-dev/FB564366-1FE4-472F-84DE-75B21EA21873%40ohsu.edu. >> >> -- >> 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+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/rdflib-dev/CAP7nqh18O_V5xibag0TJiDsoNVTOJHrSYQmyOZnt%3DHJBUF6-TA%40mail.gmail.com. > > > -- > http://gromgull.net > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rdflib-dev/CAGm1ODnOe54_3YKro_vGfs%3D%2BRRq_RqychG8jWFr5x5uVkx9BWQ%40mail.gmail.com. -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/14F6E7F4-160A-4DC6-8BBA-71E1119B0875%40gmail.com.