Search code examples
pythonrdfrdflibturtle-rdftriples

Adding triples in a certain order using RDFLib Python


I am looking to add triples in a certain order using Python's RDFLib Library. I'm aware RDF triples have no ordering, however, I was hoping to add the triples in the order in which the add() function is called. For example,

If I add the following triples

bob = URIRef("http://example.org/people/Bob")
name = Literal("Bob")

g.add((bob, RDF.type, FOAF.Person))
g.add((bob, FOAF.name, name))

The output generated

ex:Bob foaf:name "Bob"; 
       rdf:type foaf:Person. 

Would there be a way to ensure the order in which the add function is called correlates with the output file? E.g

ex:Bob rdf:type foaf:Person.
       foaf:name "Bob". 
 

Thanks for any help!


Solution

  • Unfortunately not. From the documentation,

    RDFLib graphs are un-sorted containers; they have ordinary set operations (e.g. add() to add a triple) plus methods that search triples and return them in arbitrary order.