How to print a term literal from rdflib in read mode?

I just started using rdflib, and I have a program that should get the person’s date of birth (in this example, Lewis Carroll). My program does this, 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 am sure there is an easy way to print only the date, but I could not find how to do it. if it matters to 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)
+4
source share
1 answer

The following is a way to print the date from your request:

>>> for row in qres:
...     d = str(row.asdict()['birth'].toPython())
...     print(d)
...
1832-01-27
>>>

, , - rdflib.query.ResultRow.asDict() http://rdflib.readthedocs.org/en/latest/_modules/rdflib/query.html rdflib.term.Literal.toPython(), http://rdflib.readthedocs.org/en/latest/apidocs/rdflib.html.

, row.asdict() ['birth']. toPython() - datetime.date.

+4

Source: https://habr.com/ru/post/1599664/


All Articles