Adding RDF text to graph RDFlib
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'))
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 :
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' )
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.
链接地址: http://www.djcxy.com/p/65854.html上一篇: 我需要通过Spark Graphx来完成join / joinVertices或在图形中添加一个字段
下一篇: 将RDF文本添加到图形RDFlib