Skip to content
Advertisement

Running SPARQL Queries on turtle file

I’m trying to run SPARQL queries on turtle files using python, but my sparql returns all the entries, how can I return the entry with name=”Idham Al-Taif Mahmoud”?

turtle file:

<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 code:

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)

Advertisement

Answer

I think your issue is with the methods. Here is a link with a nice example of what you want to do.

The following script works for me:

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)

Notice that I assign the results of the query to a new variable qres, and also that the parse method is void, i.e. it does not return a value like you had.

The % signs in the end are just to print a human readable result, but you can totally get rid of them.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement