我已经有一段时间没有使用 rdflib 了,正在编写一些基于 https://rdflib.readthedocs.io/en/stable/intro_to_graphs.html#basic-triple-matching
的简单代码喜欢
from rdflib import Graph
graph = Graph()
ttl_input = "dir/file.ttl"
graph.parse(ttl_input, format='ttl')
for s, p, o in graph.triples((None, None, None)):
print(f"{s} {p} {o} {o}")
我想将返回的
graph.triples
限制为对象 o
具有数据类型 xsd:anyURI
像这样:
import rdflib
from rdflib import Graph
from rdflib.namespace import NamespaceManager, XSD
graph = Graph()
ttl_input = "dir/file.ttl"
graph.parse(ttl_input, format='ttl')
for s, p, o in graph:
if o.__class__ == rdflib.term.Literal and o.datatype == XSD.anyURI:
print(f"{s} {p} {o} {o}")