0

我对使用 python 包 SPARQLWrapper 的 SPARQL 查询非常陌生。我试图使用以下查询从 DBpedia 检索结果:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>

SELECT ?s ?p
WHERE {    
    ?country a type:LandlockedCountries ;
             rdfs:label ?s ;
             prop:populationEstimate ?p .
    FILTER (?p > 15000000) .
}

我用 Python 2.7 编写的代码如下:

from SPARQLWrapper import SPARQLWrapper, JSON, POST
import sys

def main(argv):     
    endpoint = str("http://dbpedia.org/sparql") 
    # QUERY as mentioned above 
    query = str(QUERY)    
    query = query.replace("__oc__","{")
    query = query.replace("__ob__","[")
    query = query.replace("__cc__","}")
    query = query.replace("__cb__","]")
    query = query.replace("__cr__"," ")
    query = query.replace("__cn__"," ")
    print "Parsed Query: " + query
    sparql = SPARQLWrapper(endpoint)
    sparql.setQuery(query)
    sparql.setMethod(POST)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()   
    render = str("html")

    if render == "html":
        html_render(results)
    else:
        tab_render(results)

def html_render(results):

    for result in results["results"]["bindings"]:
        print result["s"]["value"], result["p"]["value"]

def tab_render(results):

    for result in results["results"]["bindings"]:
        print result["s"]["value"], result["p"]["value"]

if __name__ == '__main__':
        main(sys.argv)

我想收到一堆国名的名字和它的人口。但是,我只得到一个结果:

阿富汗 31822848

难道我做错了什么?任何形式的帮助将不胜感激。

4

0 回答 0