I'v defined some prefixes in the data.rdf file:
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://ex.org/> .
ex:Person rdf:type rdf:Class ;
ex:hasProperty ex:age.
ex:Bob rdf:type ex:Person ;
ex:hasProperty ex:age_Bob.
ex:age_Bob rdf:type ex:age;
ex:hasValue '27'^^xsd:integer .
I use the following python program to read the prefix defined in rdf:
from rdflib import Graph
g = Graph()
g.parse("data.rdf", format="turtle")
prefixes = dict(g.namespace_manager.namespaces())
for prefix, uri in prefixes.items():
if any((s, p, o) for s, p, o in g if p.startswith(uri)):
print(prefix, uri)
the result I get is:
rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#
ex http://ex.org/
Another way I thought of is to use regular expression to to extract the string, is there a better way to extract just the four prefixes defined in the rdf file instead of all namespaces?