I just started using rdflib and I have a program that needs to get the birth date of a person (in this example, Lewis Carroll). My program does that, however when I try to print the date it prints: (rdflib.term.Literal('1832-01-27', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#date')),)
I'm sure there's an easy way to print just the date but I haven't been able to find how. if it's of any relevance here's my code:
# open a graph
graph = rdflib.Graph()
# load some data
graph.parse('http://dbpedia.org/resource/Lewis_Carroll')
qres = graph.query(
("PREFIX dbpedia: <http://dbpedia.org/resource/>\n"
"PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\n"
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
"SELECT ?birth WHERE {\n"
" ?person dbpedia-owl:birthDate ?birth . \n"
" ?person foaf:givenName ?givenName . \n"
" ?person foaf:surname ?surname . \n"
" VALUES ?person { dbpedia:Lewis_Carroll }\n"
" }"
))
print("the graph has %s statements" % len(qres))
for row in qres:
print(row)
Here is a way to print the date from your query:
>>> for row in qres:
... d = str(row.asdict()['birth'].toPython())
... print(d)
...
1832-01-27
>>>
Some clues I got for solving this are rdflib.query.ResultRow.asDict() documented in http://rdflib.readthedocs.org/en/latest/_modules/rdflib/query.html and rdflib.term.Literal.toPython() documented in http://rdflib.readthedocs.org/en/latest/apidocs/rdflib.html.
Note that row.asdict()['birth'].toPython() is a datetime.date object.