RDF Parsing and Serialization#

Oxigraph provides functions to parse and serialize RDF files:

Parsing#

pyoxigraph.parse(input, mime_type, *, base_iri=None)#

Parses RDF graph and dataset serialization formats.

It currently supports the following formats:

It supports also some MIME type aliases. For example, application/turtle could also be used for Turtle and application/xml for RDF/XML.

Parameters:
  • input (io(bytes) or io(str) or str or pathlib.Path) – The binary I/O object or file path to read from. For example, it could be a file path as a string or a file reader opened in binary mode with open('my_file.ttl', 'rb').

  • mime_type (str) – the MIME type of the RDF serialization.

  • base_iri (str or None, optional) – the base IRI used to resolve the relative IRIs in the file or None if relative IRI resolution should not be done.

Returns:

an iterator of RDF triples or quads depending on the format.

Return type:

iterator(Triple) or iterator(Quad)

Raises:
>>> input = io.BytesIO(b'<foo> <p> "1" .')
>>> list(parse(input, "text/turtle", base_iri="http://example.com/"))
[<Triple subject=<NamedNode value=http://example.com/foo> predicate=<NamedNode value=http://example.com/p> object=<Literal value=1 datatype=<NamedNode value=http://www.w3.org/2001/XMLSchema#string>>>]

Serialization#

pyoxigraph.serialize(input, output, mime_type)#

Serializes an RDF graph or dataset.

It currently supports the following formats:

It supports also some MIME type aliases. For example, application/turtle could also be used for Turtle and application/xml for RDF/XML.

Parameters:
  • input (iterable(Triple) or iterable(Quad)) – the RDF triples and quads to serialize.

  • output (io(bytes) or str or pathlib.Path) – The binary I/O object or file path to write to. For example, it could be a file path as a string or a file writer opened in binary mode with open('my_file.ttl', 'wb').

  • mime_type (str) – the MIME type of the RDF serialization.

Return type:

None

Raises:
  • ValueError – if the MIME type is not supported.

  • TypeError – if a triple is given during a quad format serialization or reverse.

>>> output = io.BytesIO()
>>> serialize([Triple(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'))], output, "text/turtle")
>>> output.getvalue()
b'<http://example.com> <http://example.com/p> "1" .\n'