Warm tip: This article is reproduced from serverfault.com, please click

python-RDFLib 解析器无法识别 json-ld 格式

(python - RDFLib parser does not recognize json-ld format)

发布于 2015-08-05 19:47:35

我在 Python 3.4 中的代码:

from rdflib import Graph, plugin
import json, rdflib_jsonld
from rdflib.plugin import register, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')

context = {
"@context": {
            "foaf" : "http://xmlns.com/foaf/0.1/",
            "vcard": "http://www.w3.org/2006/vcard/ns#country-name",
            "job": "http://example.org/job",

            "name":             {"@id": "foaf:name"},
            "country":          {"@id": "vcard:country-name"},
            "profession":       {"@id": "job:occupation"},
        }
}

x = [{"name": "bert", "country": "antartica", "profession": "bear"}]
g = Graph()
g.parse(data=json.dumps(x), format='json-ld', context=context)
g.close()

错误:

"No plugin registered for (%s, %s)" % (name, kind))
rdflib.plugin.PluginException: No plugin registered for (json-ld, <class'rdflib.parser.Parser'>)

根据RDFLib 文档,支持的插件列表不包括 json-ld 格式。但是,我之前将它的格式设置为 json-ld 并且有很多使用 json-ld 格式的示例,例如:https : //github.com/RDFLib/rdflib-jsonld/issues/19

我包括了 rdflib_jsonld 的导入,尽管它之前在另一个环境(Python 2.7)上只使用了 rdflib(我知道,没有任何意义)。

第 4 行 json-ld 的 register 部分也没有帮助。

有人有想法吗?

Questioner
Brainlock
Viewed
0
Brainlock 2015-08-06 04:11:59

我通过添加它来工作:

from SPARQLWrapper import SPARQLWrapper

我正在http://rdflib.readthedocs.org/en/latest/apidocs/rdflib.plugins.sparql.results.html#module-rdflib.plugins.sparql.results.jsonlayer从 RDFLib 查看 jsonLayer 模块并注意到提到我在之前的环境中使用的 SPARQLWrapper,在那里我让示例工作并且它就在那里。