RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.14k stars 555 forks source link

Binding user-defined Namespace #2659

Closed cameronmore closed 1 month ago

cameronmore commented 9 months ago

Hello! I wrote some code that iterates through a CSV which has RDF in it like the following:

cco:Person1 , rdf:type , cco:Person
cco:Person1 rdfs:label , John

But I can't bind my user defined namespace (cco) in the graph. I've tried something like this:

g.bind('cco',cco)

And this:

g.bind('http://www.ontologyrepository.com/CommonCoreOntologies/',cco)

But neither works. Instead, ns1 is bound simply as cco and ns2 is rdfs How can I bind user-defined prefixes in a graph that don't resort to ns1 ns2?

WhiteGobo commented 7 months ago

This worked for me:

from rdflib import *

cco = Namespace("http://www.ontologyrepository.com/CommonCoreOntologies/")

g = Graph()
g.add((cco.A, BNode(), BNode()))
g.bind("cco", cco)

print(g.serialize())
#@prefix cco: <http://www.ontologyrepository.com/CommonCoreOntologies/> .
#cco:A [ ] [ ] .