如何遍历rdflib的CONSTRUCT输出? [英] How to iterate over CONSTRUCT output from rdflib?

查看:90
本文介绍了如何遍历rdflib的CONSTRUCT输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自我已经使用SPARQL CONSTRUCT查询创建了一个新图.现在,我想对其进行迭代,以便可以将语句添加到RDFlib图中,然后将数据插入到另一个Triplestore中.我有以下问题:

I've created a new graph using SPARQL CONSTRUCT query. I now want to iterate over it so that I can add the statements to an RDFlib graph and then insert the data into another triplestore. I have the following questions:

  1. 如果SPARQL CONSTRUCT返回一个图,我是否仍然需要遍历这些语句并将它们添加到RDFlib图中?我可能需要这样做,以便能够使用循环将每个三元组插入一个三元组中.
  2. 如何对SPARQL CONSTRUCT生成的图进行迭代以检索三元组? 'output''type'显示为字符串.
  1. If SPARQL CONSTRUCT returns a graph, do I still need to iterate over the statements and add them to an RDFlib graph? I probably need to do so to be able to insert each triple into a triplestore using a loop.
  2. How does one iterate over a graph resulting from SPARQL CONSTRUCT to retrieve the triples? The 'type' of 'output' shows up as string.

这是我的代码:

sesameSparqlEndpoint = 'http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name'
sparql = SPARQLWrapper(sesameSparqlEndpoint)
queryStringDownload = 'CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o FILTER REGEX(str(?s), "http")}'
dataGraph = Graph()

sparql.setQuery(queryStringDownload)
sparql.method = 'GET'
sparql.setReturnFormat(JSON)
output = sparql.query().convert()
# print output

#Print all statements in dataGraph      
for stmt in output:
    print stmt

这段代码只给了我三元组中的一列字符.

This code just gives me a single column of characters from the triples.

推荐答案

  1. 我仍然必须从通过将CONSTRUCT查询运行到RDFlib图而创建的连接图中添加语句.为什么?因为在执行INSERT时解析前者存在问题. CONSTRUCT查询的输出:a)没有包含在<>中的URI.b)不处理非ASCII字符(因此需要output.encode('utf-8')).
  2. 要从CONSTRUCT查询生成的图形中检索三元组,我们需要使用XML格式而不是上面代码中的JSON进行输出.

有趣的是:使用XML时的类型(输出)是联合图.对于我尝试过的所有其他内容,它都是一个字符串.

Interesting aside: The type(output) when using XML is Conjunctive Graph. For everything else I tried, it's a string.

这篇关于如何遍历rdflib的CONSTRUCT输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆