如何在python rdflib中附加.ttl文件



我正在使用rdflib python库进行.ttl文件处理。我在 .ttl 文件中有一些三元组,但我想通过使用 python rdflib 库向现有 .ttl 文件添加新的三元组来附加(更新(文件。例如:file1.ttl 有 10 个三元组(主语>谓词->宾语(,我想通过在 python rdflib 中附加 .ttl 文件来向同一个文件添加 5 个新的三元组。

谢谢!

你只需要按照文档"添加"图表:https://rdflib.readthedocs.io/en/4.2.2/merging.html

from rdflib import Graph
g1 = Graph()
g1.parse("file1.ttl", format="turtle")
g2 = Graph()
g2.parse("file2.ttl", format="turtle")
graph = g1 + g2
graph.serialize("file1.ttl", format="turtle")

最新更新