Python rdflib – string to URIRef or Literal – inverse of .n3() method

Question:

I’m using rdflib to work with semantic data, and want to serialise a triple to some minimal string value.

Given a triple (s,p,o), I can use rdflib’s .n3() method to return the string values representing the contents of s,p,o and join them together as a string.

" ".join([t.n3() for t in (s,p,o)])

This works whether the underlying terms are of type URIRef or Literal.

What I’d like to do next is the inverse, so given a series of strings like:

"<http://myonto.com/thing1> <http://myonto.com/prop1> <http://myonto.com/thing2>" 
"<http://myonto.com/thing1> <http://myonto.com/prop1> <2>" 

I want a method that will allow me to convert these string terms that were generated by the .n3() method back into their original rdflib typed objects (either URIRef or Literal).

Something along the lines of:

s,p,o = [inverse_n3(t) for t in longform.split( )]

Looking at the documents, I can see this term –> n3 method, but not the inverse – is this something that exists, and if so can you point me at a document covering it please?

Asked By: Thomas Kimber

||

Answers:

Turns out rdflib has this exact function hidden away in its rdflib.util library:

So I’ve ended up using:

from rdflib.util import from_n3

def n3_to_term(n3):
    return from_n3(n3.encode('unicode_escape').decode('unicode_escape'))

enabling the line to perform the stated conversion.

s,p,o = [n3_to_term(t) for t in longform.split()]

There’s some added details around serializing and deserializing to do with handling delimiters, quotes and escape characters but the core of the problem is dealt with by this rdflib.util.from_n3 function.

Answered By: Thomas Kimber
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.