Example 8: Exporting triples

This example shows how to serialize contents of a repository to a file. As usual we’ll start with obtaining a connection to the repository:

from franz.openrdf.connect import ag_connect

conn = ag_connect('python-tutorial', create=True, clear=True)

Now let’s import some data:

  <ex://s> <ex://p1> <ex://o1> , <ex://o2> ;
           <ex://p2> <ex://o3> .""")

Data can be exported by passing a file name or a file-like object as the output parameter of getStatements(). In this case we’ll want to print all statements to standard output. We can do this by passign True as the output file:

from franz.openrdf.rio.rdfformat import RDFFormat

conn.getStatements(output=True, output_format=RDFFormat.NTRIPLES)

We can see that results are printed in the specified format:

<ex://s> <ex://p1> <ex://o1> .
<ex://s> <ex://p1> <ex://o2> .
<ex://s> <ex://p2> <ex://o3> .

We can also use other arguments of getStatements() to constrain the set of exported tripes:

conn.getStatements(None, conn.createURI('ex://p1'), None,

As expected, the result contains only two triples.

<ex://s> <ex://p1> <ex://o1> .
<ex://s> <ex://p1> <ex://o2> .

A file path can also be passed as the output argument:

import os
import sys

with open('example8.nt', 'r') as f:


This outputs data read from the file:

<ex://s> <ex://p2> <ex://o3> .
<ex://s> <ex://p1> <ex://o2> .
<ex://s> <ex://p1> <ex://o1> .