Skip to content

Instantly share code, notes, and snippets.

@tatiana
Created June 28, 2012 22:19
Show Gist options
  • Save tatiana/3014365 to your computer and use it in GitHub Desktop.
Save tatiana/3014365 to your computer and use it in GitHub Desktop.
SPARQLWrapper example: List all predicates of DBPedia Band class
from SPARQLWrapper import SPARQLWrapper, JSON
query = """
SELECT distinct ?subject
FROM <http://dbpedia.org>
{
?subject rdfs:domain ?object .
<http://dbpedia.org/ontology/Band> rdfs:subClassOf ?object
OPTION (TRANSITIVE, t_distinct, t_step('step_no') as ?n, t_min (0) ).
}"""
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["subject"]["value"])
@rezacsedu
Copy link

Hi Tatiana,

Thanks for your nice work. If I want to print the query result in JSON format, I have to put the variable name manually in the print statement which is a major drawback of this approach. Is there any way to print the result as just tabular/text format? For example please see the attached image

untitled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment