Spycologue
Spycologue

Reputation: 1

Malformed SPARQL query in python because of bad encoding of terms with "é" letter or spaces

I want to perform an insert query with python using sparqlwrapper. But when I perform my query, I get an error message telling me that my query is malformed. I suspect the error to be some special accent character in French.

Here is what I did :

db = SPARQLWrapper(config.SPARQL_UPDATE_ENDPOINT)

query="""PREFIX : <http://www.my_ontologie.owl>
INSERT DATA {
GRAPH <http://my_graph.com>{
:athlétisme :hasGender :Masculine;
:PartOfSpeech :CommonNoun;
:hasDefinition "discipline sportive regroupant les sports pratiqués dans un stade : course, saut, lancer...";
:hasFrenchPhoneticForm "ATLETISME";:hasCapitalForm "ATHLÉTISME".
}
}"""
print(query)
db.setMethod('POST')
db.setQuery(query)
db.query()

Here is the error I get :

SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: A bad request has been sent to the endpoint: probably the SPARQL query is badly formed. 

Response:
b'MALFORMED QUERY: Lexical error at line 1, column 135.  Encountered: "\\u00a9" (169), after : ""'

After some researches, I found out it was some encoding error and I tried to encode my query withquery.encode('utf-8') or query.encode('unicode-escape') but it gives me other encoding errors. I looked into the documentation of Sparqlwrapper but I didn't find anything to help me. By the way, when I perform this request on graphDB in the SPARQL editor, the terms are added to the database without any issue.

Upvotes: 0

Views: 199

Answers (1)

Spycologue
Spycologue

Reputation: 1

I think I solved the problem by putting the request in an f string and using the function quote()on the words but my database is now filled with words %C3%89 instead of "é" and %20 instead of spaces. And I was not able to reproduce the other error code but it was in the form b'MALFORMED QUERY: Lexical error at line 1, column 135. Encountered: "\" (169), after : ""'

Upvotes: 0

Related Questions