简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM