It is not very difficult. Firstly, some necessary imports:
from StringIO import StringIO from rdflib import Graph, URIRef
I use StringIO here to avoid creating a file. Instead, I simply listed the 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 into it:
graph = rdflib.Graph() for line in tabfile: triple = line.split()
Now you have the entire graph in memory (if you have enough memory, of course). Now you can print it:
print graph.serialize(format='nt') # prints: # <subject1> <predicate1> <object1> . # <subject2> <predicate2> <object2> .
source share