3

How do I add RDF text that has been assigned to a variable (this text has also been validated and is correct RDF) to a graph in RDFlib?

I have a variable assigned to a bunch of text in RDf format like below:

block = '''<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:lem="http://www.example.eu/lem#" xmlns:lexinfo="http://www.sample.net/2.0/info#">
    <lem:Lexicon rdf:about="http://myserver/myns#lex">
.......................
.......................
</rdf:RDF>'''

I know I could write this to a file and then read that file into the graph but I was wondering is there a way of adding this to the graph in the same file (like you would do for triples) similar to this:

graph.add(block)

The end game is to display them in N3 format using this:

print (wordnet.graph.serialize(format='n3'))

2 Answers 2

4

The RDFLib Graph.parse method will accept a 'data' keyword parameter that is a string.

#Where g is your graph.
g.parse(data=block, format="application/rdf+xml")

There is an example in the RDFLib Graph docstrings.

Update: I updated my answer to include the proper example per comment below. Used tagged release 4.1.2 in case master changes.

Sign up to request clarification or add additional context in comments.

2 Comments

The line you linked to just has stream.close(). I think line 207 is a better example.
This is a better answer. I wish that the documentation (which I cited in my answer) explained what the data keyword parameter was for. (Update: it appears to now. Maybe it didn't when I wrote the original answer?) It would have saved some trouble.
3

In Java, using Jena, I do things like this using a ByteArrayInputStream to create an input stream from the RDF text that I can then pass to the parsing functions (which typically accept both URIs and InputStreams. The docs for Graph.parse say that

parse(source=None, publicID=None, format=None, location=None, file=None, data=None, **args)

Parameters:

  • source: An InputSource, file-like object, or string. In the case of a string the string is the location of the source.

Update: lawlesst provided an answer that points out that the data parameter to Graph.parse can be a string containing RDF content. The documentation mentions that now, but I don't think it did when I originally answered. lawlesst's answer should be preferred, in my opinion.

So to apply the same idiom, you just need to create an InputSource or “file-like object” from your string. I'm not much of a Pythonista, so I originally thought you'd want to take the InputSource route, but it appears that that's a SAX parser thing. What you probably want is Python's StringIO:

7.5. StringIO — Read and write strings as files

This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations (section File Objects). (For standard strings, see str and unicode.)

class StringIO.StringIO([buffer])
When a StringIO object is created, it can be initialized to an existing string by passing the string to the constructor. If no string is given, the StringIO will start empty. In both cases, the initial file position starts at zero.

So it seems like if you create a StringIO with your given text, then you can pass it as the first argument to Graph.parse. As I said, I'm not really a Python user, but I think it would look something like this:

block = '''<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
 …
</rdf:RDF>'''
Graph g;
g.parse( StringIO.StringIO(block), format='xml')

Update: Based on lawlesst's answer this can be simplified without the StringIO as just:

g.parse( data=block, format='xml' )

2 Comments

The 'data' keyword option in parse() allows you to directly point to your string without the need to use StringIO (see the answer of @lawlesst below)
Well designed packages accept a stream as an alternative to a file, so this suggestion works. Sadly rdflib does not. If your input is XML you can provide an xml.sax.xmlreader.InputSource, so something like this suggestion works. But for N3, your choices are string data or a file. The sample code writes data to a file before ingesting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.