如何确定或过滤 rdflib 文字的数据类型?

问题描述 投票:0回答:1

我已经有一段时间没有使用 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

python rdf rdflib
1个回答
0
投票

像这样:

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}")

© www.soinside.com 2019 - 2024. All rights reserved.