RDFLib / sparqlwrapper

A wrapper for a remote SPARQL endpoint
https://sparqlwrapper.readthedocs.io/
Other
520 stars 122 forks source link

WikiData Not Returning CSV #226

Open hendursaga opened 1 year ago

hendursaga commented 1 year ago

While I can request WikiData to return JSON easily on SELECT queries, I cannot do the same for CSV/TSV, it just returns XML and then sparqlwrapper complains it's not in a format it's expecting.

I think the problem has to do with setting URL parameters like format and maybe results or output. See this article and its referenced link.

I'm not sure if that's correct behavior or not on WikiData's side, but it would be nice to have at least a workaround.

inormann commented 1 year ago

Indeed, the problem seems to be that format parameter added by SPARQLWrapper. Fortunately the class comes with a method setOnlyConneg to suppress these parameters and to rely on content negotiation only.

This seems to be the solution as this code snippet may demonstrate:

from SPARQLWrapper import SPARQLWrapper, CSV

sparql = SPARQLWrapper("https://query.wikidata.org/sparql", returnFormat=CSV)
sparql.setOnlyConneg(True) # enforce content negotiation only!
sparql.setQuery('SELECT * WHERE {?s ?p ?o} LIMIT 3')
ret = sparql.query()
ret._get_responseFormat() # -> 'csv'