查询SPARQLWrapper的问题

时间:2016-03-03 21:29:44

标签: json python-2.7 sparql dbpedia sparqlwrapper

我是使用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

我做错了吗?任何形式的帮助将受到高度赞赏。

0 个答案:

没有答案