python - 如何在 Python 中使用 RDFlib 从字典列表(三元组)中制作多个 RDF 图?
问题描述
我所拥有的:需要使用 RDFlib 将其转换为 RDF 的 20k 字典(三元组)列表。
目标:对于每一个三元组,我想要一个新的 rdf Graph()。但是,当我遍历所有三元组时,方法 .add() 会将每个三元组累积到同一个图中。
问题:如何在每个循环中制作一个新图形,以便图形不会在每个循环中累积数据?也许有一种方法可以在每个循环中删除图表?
在实际代码中,我将每个 rdf 转换的三元组发布到一个服务中,这需要一个一个地完成,因为它有很多数据(20k 三元组)。对于每一个三元组,它都是它自己的出版物,所以我不能将它们全部放在同一张图中。
我的示例代码(简化):
import rdflib
from rdflib import Graph, FOAF, Literal, Namespace
list_dicts_triples = [{'subject': 'she', 'predicate': 'went', 'object': 'there'},
{'subject': 'I', 'predicate': 'like', 'object': 'ice-cream'}]
graph = Graph()
EX = Namespace('http://example.org/')
rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
graph.bind('ex', EX)
subject_rdf = rdf["subject"]
object_rdf = rdf["object"]
predicate_rdf = rdf['predicate']
for triple in list_dicts_triples:
graph.add((subject_rdf, EX.TYPE, FOAF.Person)) #subject
graph.add((predicate_rdf, rdflib.URIRef('http://example_predicates.com'), Literal(triple['predicate']))) #predicate
graph.add((object_rdf, rdflib.URIRef('http://example_concepts.com'), Literal(triple['object']))) #object
output = graph.serialize(format='ttl').decode('u8')
print(output) #print rdf graph
当前输出:(如您所见,第一个字典的项目已添加到第二个 rdf 图('there','went'..)。
#graph 1
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
rdf:object ns1:example_concepts.com "there" .
rdf:predicate ns1:example_predicates.com "went" .
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .
#graph 2
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
rdf:object ns1:example_concepts.com "ice-cream",
"there" .
rdf:predicate ns1:example_predicates.com "like",
"went" .
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .
期望的输出:(这里,每个图代表列表的每个唯一字典/三元组。
#graph 1
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
rdf:object ns1:example_concepts.com "there" .
rdf:predicate ns1:example_predicates.com "went" .
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .
#graph 2
@prefix ex: <http://example.org/> .
@prefix ns1: <http://> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
rdf:object ns1:example_concepts.com "ice-cream".
rdf:predicate ns1:example_predicates.com "like".
rdf:subject ex:TYPE <http://xmlns.com/foaf/0.1/Person> .
解决方案
在上面的代码中,您不会为每个三元组创建一个新图表。请注意,声明在 for 循环之外。将其移入内部,它应该可以按照您的意愿工作。
import rdflib
from rdflib import Graph, FOAF, Literal, Namespace
list_dicts_triples = [{'subject': 'she', 'predicate': 'went', 'object': 'there'},
{'subject': 'I', 'predicate': 'like', 'object': 'ice-cream'}]
EX = Namespace('http://example.org/')
rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
subject_rdf = rdf["subject"]
object_rdf = rdf["object"]
predicate_rdf = rdf['predicate']
for triple in list_dicts_triples:
graph = Graph()
graph.bind('ex', EX)
graph.add((subject_rdf, EX.TYPE, FOAF.Person)) #subject
graph.add((predicate_rdf, rdflib.URIRef('http://example_predicates.com'), Literal(triple['predicate']))) #predicate
graph.add((object_rdf, rdflib.URIRef('http://example_concepts.com'), Literal(triple['object']))) #object
output = graph.serialize(format='ttl').decode('u8')
print(output) #print rdf graph
推荐阅读
- php - 购物车篮
- php - 缓存目录权限设置为root
- angular - Angular 6 中使用 Observable 的 $q.when、$q.defer 和 defer.promise 的替代品是什么
- ssis - SSDT 2017 部署自定义组件错误:无法添加到数据流任务
- c# - 将循环中的 i 与下一个值进行比较并访问最后一个值
- c++ - 从复制构造函数调用构造函数
- core-image - componentMax CIBlendKernel 与lighten 或lighterColor 有何不同?
- javascript - 单击另一个页面时,导航栏活动不会改变
- python - cx_freeze [Errno 2] 没有这样的文件或目录
- python - 在Apache中加载Python时必须添加执行权限?