Creating an RDF file using the txt file as input

How to convert a simple txt file with tab-delimited (containing header object, predicate, object) to N-triple RDF format using python RDFlib module?

+6
source share
1 answer

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() # 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 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> . 
+10
source

Source: https://habr.com/ru/post/906853/


All Articles