我刚开始使用 rdflib,我有一个程序需要获取一个人的出生日期(在本例中为 Lewis Carroll)。我的程序会这样做,但是当我尝试打印它打印的日期时: (rdflib.term.Literal('1832-01-27', datatype=rdflib.term.URIRef(' http://www.w3.org/2001/XMLSchema#date ')),)
我确信有一种简单的方法可以只打印日期,但我一直没能找到方法。 如果它有任何相关性,这是我的代码:
# 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)
最佳答案
这是一种打印查询日期的方法:
>>> 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 对象。
关于python - 如何以可读的方式从 rdflib 打印术语文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629462/