1

在 rdflib 命名空间中创建 unicode 项时出现 Unicode 解码错误

$ 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)

请帮助解决这个问题。

4

1 回答 1

2

我不知道 rdflib,但是当你写的时候:

item = "Petra Němcová"

您正在创建普通字符串对象 - 尝试使用以下方式定义 unicode:

item = u"Petra Němcová"

如评论 - 如果您已经将项目作为字符串,并且您知道它的编码,请使用(如果编码是 utf-8):

item = item.decode('utf-8')
于 2013-03-29T22:08:37.323 回答