将`rdflib.dataset`序列化为默认图?
我希望通过使用 rdflib.dataset
将数据解析到默认图中,通过使用 ds.add(...)
或 ds.parse(... )
。当我尝试序列化到 application/trig
或 application/n-quads
时,我可以看到默认的图形三元组在空白节点下。
有什么方法可以强制添加 ds.add(...)
或 ds.parse(...)
位于默认图下的三元格?
示例:
from rdflib import Dataset
from rdflib.term import URIRef
ds = Dataset()
# Parse into Default Graph.
ds.parse(data='<df:s> <df:p> <df:o>.')
# Parse into Named Graph '<ng:g>'.
g = ds.graph(URIRef('ng:g'))
g.parse(data='<ng:s> <ng:p> <ng:o>.')
ds_nquads = ds.serialize(format='nquads')
print(ds_nquads)
输出:
<ng:s> <ng:p> <ng:o> <ng:g> .
<df:s> <df:p> <df:o> _:Nbd6d6df5e4cb4348980c2d6ff511d6ce .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
g.add()
将三元组添加到Graph
,g
,但d.add()
可以添加四Quads到dataset
,d
,因此您当然可以使用add((主题,谓词,对象,图形))
,请参阅=“ https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html?highlight = dataset#rdflib.graph.dataset.dataset” rel =“ nofollow noreflow noreferrer”> noreflow noreferrer“> dataset document 。还尝试使用数据集示例文件,其中显示了大多数数据集操作:
g.add()
adds triples to aGraph
,g
, butd.add()
can add quads to aDataset
,d
, so you can certainly useadd((subject, predicate, object, graph))
, see the Dataset documentation.Try also the Dataset example file, it's got most Dataset operations shown in it: https://github.com/RDFLib/rdflib/blob/master/examples/datasets.py
我认为问题可能在于
parse()
与dataset
一起使用。三元组似乎没有像人们期望的那样进入数据集中的默认图,而是陷入了空白节点识别的图。至于解决方法,如果您使用
dataset.add()
,用三倍而不是Quads调用它,它们将转到默认图,如在此处的TRIG输出中可以看出:示例1(示例1)添加,使用TRIG输出):
输出1:
但是,N-Quads序列化可能还有另一个问题,因为它似乎没有省略输出的默认图标签,而是显示RDFLIB
DataSet
内部默认图的URI。 (上面的Trig序列化确实忽略了它,但是在输出的名称空间绑定中可见了一个痕迹。)示例2(添加,N-Quads输出,输出):
输出2:
用RDFLIB 6.1.1对其进行了测试。
I think that the problem might be in how
parse()
works with aDataset
. Triples do not seem to go into the default graph in the Dataset as one would expect but into a graph identified by a blank node.As for a workaround, if you use
Dataset.add()
, calling it with triples instead of quads, they will go to the default graph, as can be seen from the trig output here:Example 1 (add, with trig output):
Output 1:
However, the n-quads serialization might have another issue, since it does not seem to omit the default graph label from the output, instead showing the rdflib
Dataset
internal URI for the default graph. (The trig serialization above did omit it, but a trace of it was visible in the namespace bindings that it outputted.)Example 2 (add, with n-quads output):
Output 2:
These were tested with rdflib 6.1.1.