Bert Carremans
Bert Carremans

Reputation: 1733

Syntax of SPARQL query in Python to query ttl file

I am trying to query a (originally .ttl) file with the RDFLib in Python. An extract from the file is shown below.

http://id.vlaanderen.be/statistieken/dq/kubus-kadaster/observatie/0/0/0#id a qb:Observation ; qb:dataSet http://id.vlaanderen.be/statistieken/dq/kubus-kadaster#id ; statsvl:refArea http://id.fedstats.be/nis/11001#id ; statsvl:timePeriod http://id.vlaanderen.be/statistieken/concept/jaar_1997#id ; statsvl:oppervlaktetype http://id.vlaanderen.be/statistieken/concept/appartementen#id ; sdmx-attribute:unitMeasure unit:Euro ; qb:measureType statsvl:totaleki ; statsvl:totaleki "916371"^^xsd:int .

I want to extract the value of totaleki for this example. I am using the SPARQLWrapper to do this. However, I think there is something wrong with the WHERE clause. Does anyone know how I can get this value for this specific refArea?

import rdflib
from SPARQLWrapper import SPARQLWrapper, JSON

g = rdflib.Graph()
result = g.parse('cube7.ttl', format='n3')

sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX statsvl: <http://id.vlaanderen.be/statistieken/def#>
SELECT ?refArea ?totaleki
WHERE { <http://id.fedstats.be/nis/11001#id> statsvl:refArea ?refArea 
        statsvl:totaleki ?totaleki}
""")

This produces the error: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed.

Upvotes: 0

Views: 1116

Answers (1)

UninformedUser
UninformedUser

Reputation: 8465

Semicolon is missing after the first triple pattern:

PREFIX statsvl: <http://id.vlaanderen.be/statistieken/def#>
SELECT ?refArea ?totaleki
WHERE { <http://id.fedstats.be/nis/11001#id> statsvl:refArea ?refArea ;
                                             statsvl:totaleki ?totaleki .
}

Upvotes: 1

Related Questions