Skip to content

Instantly share code, notes, and snippets.

@KMax
Created February 15, 2014 11:22
Show Gist options
  • Save KMax/9017914 to your computer and use it in GitHub Desktop.
Save KMax/9017914 to your computer and use it in GitHub Desktop.
Problem in saving an unicode literal in a sparql store
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import rdflib
import urllib
import traceback
from rdflib.namespace import FOAF, RDF
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore
def save(graph, name):
try:
agent = rdflib.URIRef('http://example.com/resource/1')
graph.add((agent, RDF.type, FOAF.Agent))
graph.add((agent, FOAF.name, rdflib.Literal(name)))
except:
print "\n====== Can't save " + repr(name) + " ======\n"
traceback.print_exc()
def main():
store = SPARQLUpdateStore(
queryEndpoint = "http://<host>/openrdf-sesame/repositories/<repository>",
update_endpoint = "http://<host>/openrdf-sesame/repositories/<repository>/statements",
context_aware = False)
graph = rdflib.Graph(store)
save(graph, 'Raphaël Troncy')
save(graph, u'Raphaël Troncy')
if __name__ == '__main__':
main()
====== Can't save 'Rapha\xc3\xabl Troncy' ======
Traceback (most recent call last):
File "./example.py", line 14, in save
graph.add((agent, FOAF.name, rdflib.Literal(name)))
File "/usr/lib/python2.7/site-packages/rdflib/graph.py", line 397, in add
self.__store.add((s, p, o), self, quoted=False)
File "/usr/lib/python2.7/site-packages/rdflib/plugins/stores/sparqlstore.py", line 571, in add
r = self._do_update(q)
File "/usr/lib/python2.7/site-packages/rdflib/plugins/stores/sparqlstore.py", line 631, in _do_update
update = urllib.urlencode({'update': update})
File "/usr/lib64/python2.7/urllib.py", line 1312, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xeb' in position 85: ordinal not in range(128)
====== Can't save u'Rapha\xebl Troncy' ======
Traceback (most recent call last):
File "./example.py", line 14, in save
graph.add((agent, FOAF.name, rdflib.Literal(name)))
File "/usr/lib/python2.7/site-packages/rdflib/graph.py", line 397, in add
self.__store.add((s, p, o), self, quoted=False)
File "/usr/lib/python2.7/site-packages/rdflib/plugins/stores/sparqlstore.py", line 571, in add
r = self._do_update(q)
File "/usr/lib/python2.7/site-packages/rdflib/plugins/stores/sparqlstore.py", line 631, in _do_update
update = urllib.urlencode({'update': update})
File "/usr/lib64/python2.7/urllib.py", line 1312, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xeb' in position 85: ordinal not in range(128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment