运行 turtle 文件的 SPARQL 查询
Running SPARQL Queries on turtle file
我正在尝试 运行 使用 python 对 turtle 文件进行 SPARQL 查询,但是我的 sparql returns 所有条目,我如何 return 条目名称="Idham Al-Taif Mahmoud"?
乌龟档案:
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/hasName> "Idham Al-Taif Mahmoud".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/diedOn> "28 Feb 2017".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/hasAge> "adult".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/hasSex> "male".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/diedIn> "Samarra, south of Salah Al-Din".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/hasName> "Shukran Ghanim Hussein".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/diedOn> "26 Feb 2017".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/hasAge> "child".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/hasSex> "male".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/diedIn> "Hay Koor, west Mosul".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasName> "Munther Al-Ajaj ".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasMaritalStatus> "married".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasParentalStatus> "parent".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/diedOn> "25 Feb 2017".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasAge> "adult".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasSex> "male".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/diedIn> "Al-Mazra'a village, Baiji, north of Salah Al-Din".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/hasName> "Shifa Gerdi/Born Shifa Zikri Ibrahim Gerdi".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/diedOn> "25 Feb 2017".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/hasAge> "young adult".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/hasSex> "female".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/diedIn> "west Mosul".
python代码:
import rdflib
filename = "turtle.ttl"
g = rdflib.Graph()
result = g.parse(filename, format='ttl')
print(result)
query = """
SELECT ?person
WHERE {
?person <http://dbpedia.org/ontology/hasName> "Idham Al-Taif Mahmoud"
}
"""
g.query(query)
for stmt in g:
print(stmt)
我认为您的问题出在方法上。 Here 是一个 link,其中包含一个很好的示例,说明您想要做什么。
以下脚本适合我:
import rdflib
filename = "turtle.ttl"
g = rdflib.Graph()
g.parse(filename, format='ttl')
query = """
SELECT ?person
WHERE {
?person <http://dbpedia.org/ontology/hasName> "Idham Al-Taif Mahmoud"
}
"""
qres = g.query(query)
for row in qres :
print("%s" % row)
请注意,我将查询结果分配给一个新变量 qres
,而且 parse
方法是无效的,即它不会 return 一个像你这样的值有。
最后的%
标志只是为了打印一个人类可读的结果,但你完全可以去掉它们。
我正在尝试 运行 使用 python 对 turtle 文件进行 SPARQL 查询,但是我的 sparql returns 所有条目,我如何 return 条目名称="Idham Al-Taif Mahmoud"?
乌龟档案:
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/hasName> "Idham Al-Taif Mahmoud".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/diedOn> "28 Feb 2017".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/hasAge> "adult".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/hasSex> "male".
<http://example.org/person/a6376-dh3642> <http://dbpedia.org/ontology/diedIn> "Samarra, south of Salah Al-Din".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/hasName> "Shukran Ghanim Hussein".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/diedOn> "26 Feb 2017".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/hasAge> "child".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/hasSex> "male".
<http://example.org/person/a6363-fn3690> <http://dbpedia.org/ontology/diedIn> "Hay Koor, west Mosul".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasName> "Munther Al-Ajaj ".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasMaritalStatus> "married".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasParentalStatus> "parent".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/diedOn> "25 Feb 2017".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasAge> "adult".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/hasSex> "male".
<http://example.org/person/a6348-sz3469> <http://dbpedia.org/ontology/diedIn> "Al-Mazra'a village, Baiji, north of Salah Al-Din".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/hasName> "Shifa Gerdi/Born Shifa Zikri Ibrahim Gerdi".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/diedOn> "25 Feb 2017".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/hasAge> "young adult".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/hasSex> "female".
<http://example.org/person/a6381-xz3480> <http://dbpedia.org/ontology/diedIn> "west Mosul".
python代码:
import rdflib
filename = "turtle.ttl"
g = rdflib.Graph()
result = g.parse(filename, format='ttl')
print(result)
query = """
SELECT ?person
WHERE {
?person <http://dbpedia.org/ontology/hasName> "Idham Al-Taif Mahmoud"
}
"""
g.query(query)
for stmt in g:
print(stmt)
我认为您的问题出在方法上。 Here 是一个 link,其中包含一个很好的示例,说明您想要做什么。
以下脚本适合我:
import rdflib
filename = "turtle.ttl"
g = rdflib.Graph()
g.parse(filename, format='ttl')
query = """
SELECT ?person
WHERE {
?person <http://dbpedia.org/ontology/hasName> "Idham Al-Taif Mahmoud"
}
"""
qres = g.query(query)
for row in qres :
print("%s" % row)
请注意,我将查询结果分配给一个新变量 qres
,而且 parse
方法是无效的,即它不会 return 一个像你这样的值有。
最后的%
标志只是为了打印一个人类可读的结果,但你完全可以去掉它们。