すべてのエントリを繰り返し処理せずに対象を知ってオブジェクトをルックアップする方法はありますか?
答えははいです。また、さまざまなメカニズムを使用することができます:(a)制限付きで反復する。または(b)SPARQLクエリを発行する。
(a)は、グラフを制約し、この溶液をグラフオブジェクト上RDFLib triples
関数を使用
反復します。 this referenceを参照してください。
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
subject = article = rdflib.term.URIRef("http://www.someuri.org/for/your/subject")
# (subject,None,None) represents a constrain to iterate over the graph. By setting
# any of the three elements in the triple you constrain by any combination of subject,
# predicate or object. In this case we only constrain by subject.
for triple in g.triples((subject,None,None)):
print triple
(b)はSPARQLクエリ
SPARQL standardを使用して、より標準溶液を発行します。
rdflib.plugin.register('sparql', rdflib.query.Processor,
'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
'rdfextras.sparql.query', 'SPARQLQueryResult')
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
query = """
SELECT ?pred ?obj WHERE {
<http://www.someuri.org/for/your/subject> ?pred ?obj
}
"""
for row in g.query(query):
print "Predicate:%s Object:%s"%(row[0],row[1])
ありがとう!私は最初のオプションをかなり簡潔に選んでいますが、SPARQLEを他の目的に使うつもりです。とても役に立ちました – user52028778