3

我正在使用rdflib(包 python-rdflib 2.4.2-3,Python 2.7.5+,Ubuntu 13.10)非常沮丧。我只是想将两个 NT 文件加载到本地三重存储中。这是代码:

from rdflib import Graph

graph = Graph('Sleepycat')
rt = graph.open(fn)
# everything is fine, triplestore open.

# try to parse some files
ex = "http://dbpedia.org/data3/Place.ntriples"
# ex = "http://dbpedia.org/data3/Place.n3" # another option
# none of the following works
g.parse( ex )
g.parse( ex, "n3" )
g.parse( ex, "nt" )
g.parse( ex, "ntriple" )
g.parse( ex, "thisisrubbish" )

此代码总是引发xml.sax._exceptions.SAXParseException: http://dbpedia.org/data3/Place.ntriples:1:6: not well-formed (invalid token). 很明显,parse默认为 RDF 格式,试图将文本解析为 XML(并且失败)。如最后一行所示,代码不检查格式是否存在。它只是忽略它。

另一个令人讨厌的方面是,这parse似乎从图中删除了所有内容,这不是文档中描述的行为:

graph = Graph('Sleepycat')
graph.open("somewhere.db")

graph.parse(input1) # graph contains input1
graph.parse(input2) # graph contains only input2, but should contain input1+input2.

我不得不承认这个库看起来太错误了,无法使用。

关于如何在 Python 中调试这个和/或替代方案的任何想法?

穆龙

4

1 回答 1

3

You need to use the format keyword when calling parse:

g.parse( ex, format="nt" )

As you noticed parse will default to RDF/XML

I would also recommend upgrading from rdflib 2.4.2-3 to the latest version, 4.1.2. You can get this with easy_install or pip. The docs you reference are for version 4+.

于 2014-05-09T10:38:49.127 回答