Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WikiData Not Returning CSV #226

Open
hendursaga opened this issue Mar 5, 2023 · 1 comment
Open

WikiData Not Returning CSV #226

hendursaga opened this issue Mar 5, 2023 · 1 comment

Comments

@hendursaga
Copy link

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
Copy link

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants