0

I am trying to lookup the names of Italian Senators in a public SPARQL endpoint.

Starting sample data is the following set:

longnames = ['Lars Danielsson', 'Giorgia Meloni', 'Ursula von der Leyen', 'Filippo Mannino', 'Matteo Piantedosi', 'Lamberto Giannini']

and here the relevant part of my code:

from SPARQLWrapper import SPARQLWrapper, JSON
endpoint = "http://dati.senato.it/sparql"
sparql = SPARQLWrapper(endpoint)
sparql.setReturnFormat(JSON)

for label in longnames:
    sparqlquery = f"""
    PREFIX osr:<http://dati.senato.it/osr/>
    SELECT * WHERE {{
        ?uri a osr:Senatore. 
        ?uri rdfs:label '{label}'^^xsd:string.
    }}
    """
    print(f"{endpoint} ---> {sparqlquery}")
    sparql.setQuery(sparqlquery)
    try:
        ret = sparql.queryAndConvert()
        for r in ret["results"]["bindings"]:
            print(r)
    except Exception as e:
        print(e)

Please note that I am building the SPARQL query using an f-string. Sadly this code gives an error I'm not understanding despite the line printing the endpoint and the query looks perfectly allright. Here's the error:

QueryBadFormed: A bad request has been sent to the endpoint: probably the SPARQL query is badly formed.

Response:
b"Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Invalid character in SPARQL expression at '%'\n\nSPARQL query:\ndefine sql:big-data-const 0 \n#output-format:application/sparql-results+json\n%0A PREFIX osr%3A%3Chttp%3A//dati.senato.it/osr/%3E%0A SELECT %2A WHERE %7B%0A %3Furi a osr%3ASenatore. %0A %3Furi rdfs%3Alabel %27Ursula von der Leyen%27%5E%5Exsd%3Astring.%0A %7D%0A "

What am I doing wrong?

Robert Alexander
  • 875
  • 9
  • 24
  • Guess I've figured it out by myself. Leaving this for anyone with the same problem. I changed the quotes around the f-string curly braces to a double quote and it seems to work. – Robert Alexander Jan 15 '23 at 09:36
  • Nope ... now get errors with the same code. Start thinking problem is on the endpoint and not in my code. Hope anyone can try and confirm, – Robert Alexander Jan 15 '23 at 09:40

0 Answers0