Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a RDF file using a txt file as input

How can I convert a simple tab-delimited txt file (containing the headers subject, predicate, object) into a RDF N-triple format using the python module RDFlib?

like image 212
EvenStar Avatar asked Jan 25 '12 12:01

EvenStar


1 Answers

It's not very complicated. First, some necessary imports:

from StringIO import StringIO
from rdflib import Graph, URIRef

I'm using StringIO here to avoid creating a file. Instead, I'll just list some contents and a file-like object with these contents:

contents = '''\
subject1\tpredicate1\tobject1
subject2\tpredicate2\tobject2'''  
tabfile = StringIO(contents)

Then create a graph and load all triples to it:

graph = rdflib.Graph()

for line in tabfile:
    triple = line.split()                # triple is now a list of 3 strings
    triple = (URIRef(t) for t in triple) # we have to wrap them in URIRef
    graph.add(triple)                    # and add to the graph

Now you have the whole graph in memory (assuming you have enough memory, of course). You can now print it:

print graph.serialize(format='nt')

# prints:
# <subject1> <predicate1> <object1> .
# <subject2> <predicate2> <object2> .
like image 120
DzinX Avatar answered Sep 23 '22 10:09

DzinX