Question

I am getting a Unicode decode error while creating unicode item in rdflib namespace

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from rdflib.graph import ConjunctiveGraph
>>> from rdflib import Namespace, BNode, Literal, RDF, URIRef
***>>> rdfsNS = Namespace("http://www.w3.org/2000/01/rdf-schema#")
>>> item = "Petra Němcová"
>>> i = rdfsNS[item]***
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/rdflib/namespace.py", line 88, in __getitem__
    return self.term(key)
  File "/usr/local/lib/python2.7/dist-packages/rdflib/namespace.py", line 85, in term
    return URIRef(self + name)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 7: ordinal not in range(128)

Please help resolving this issue.

Was it helpful?

Solution

I don't know rdflib, but when you write:

item = "Petra Němcová"

you are creating normal string object - try to define unicode with:

item = u"Petra Němcová"

As in comment - if you already have item as string, and you know its encoding, use (if encoding was utf-8):

item = item.decode('utf-8')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top