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: [email protected]<mailto:[email protected]>
ORCiD: https://orcid.org/0000-0001-5059-4132


From: <[email protected]> on behalf of Robert Alexander 
<[email protected]>
Reply-To: "[email protected]" <[email protected]>
Date: Friday, January 7, 2022 at 11:17 AM
To: rdflib-dev <[email protected]>
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><https://urldefense.com/v3/__http:/purl.org/dc/terms/subject*3E__;JQ!!Mi0JBg!bxw1FRIizJnQ0o2yyvMLGOJADoq06M086Tbb4tbYH8JWoNveq21AnC8S-ReoEb8$>
 ?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<https://urldefense.com/v3/__http:/github.com/RDFLib__;!!Mi0JBg!bxw1FRIizJnQ0o2yyvMLGOJADoq06M086Tbb4tbYH8JWoNveq21AnC8ScFdqFBQ$>
---
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 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rdflib-dev/e6752549-3d70-413d-9844-338a76c2cfd2n%40googlegroups.com<https://urldefense.com/v3/__https:/groups.google.com/d/msgid/rdflib-dev/e6752549-3d70-413d-9844-338a76c2cfd2n*40googlegroups.com?utm_medium=email&utm_source=footer__;JQ!!Mi0JBg!bxw1FRIizJnQ0o2yyvMLGOJADoq06M086Tbb4tbYH8JWoNveq21AnC8SIxGTAwM$>.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rdflib-dev/FB564366-1FE4-472F-84DE-75B21EA21873%40ohsu.edu.

Reply via email to