Table of contents
- The context
- What I want to do
- Why I want to do this?
The context
I know how to generate a Turtle file using Python and rdflib
. See minimal working example below. It generates a file called output.txt
in Turtle format.
import rdflib
g = rdflib.Graph()
g.add((rdflib.URIRef('http://example.org/my_subject_1'),
rdflib.URIRef('http://example.org/my_predicate_1'),
rdflib.URIRef('http://example.org/my_object_1')))
g.add((rdflib.URIRef('http://example.org/my_subject_1'),
rdflib.URIRef('http://example.org/my_predicate_2'),
rdflib.URIRef('http://example.org/my_object_1')))
g.serialize('output.txt', format='turtle')
$ source venv/bin/activate
$ python main.py
$ cat output.txt
@prefix ns1: <http://example.org/> .
ns1:my_subject_1 ns1:my_predicate_1 ns1:my_object_1 ;
ns1:my_predicate_2 ns1:my_object_1 .
What I want to do
There are some changes that I'd like to do to the output of serialize
.
- Make every pair of predicate and object to be shown in a separated line. That is, the output should look as in the code block below.
@prefix ns1: <http://example.org/> .
ns1:my_subject_1
ns1:my_predicate_1 ns1:my_object_1 ;
ns1:my_predicate_2 ns1:my_object_1 .
- Make every pair of predicate and object have an indentation of two spaces, The default is 4 spaces. The output should look as shown in the code block below.
@prefix ns1: <http://example.org/> .
ns1:my_subject_1
ns1:my_predicate_1 ns1:my_object_1 ;
ns1:my_predicate_2 ns1:my_object_1 .
- Remove the space character after objects in subject-predicate-object triples.
@prefix ns1: <http://example.org/>.
ns1:my_subject_1
ns1:my_predicate_1 ns1:my_object_1;
ns1:my_predicate_2 ns1:my_object_1.
In summary, I would like serialize
to generate the output as shown in the last code block.
Why I want to do this?
I'm generating some turtle files containing a lot of information. Sometimes I'll need to edit those files manually, so I wish them to have an structure that I feel it's more readable to me.