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>
> <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 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
> <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 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
> <https://groups.google.com/d/msgid/rdflib-dev/FB564366-1FE4-472F-84DE-75B21EA21873%40ohsu.edu?utm_medium=email&utm_source=footer>
> .
>

-- 
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.

Reply via email to