My favorites | Sign in
Project Logo
             
Search
for
Updated Dec 01, 2007 by drewpca
Labels: Intro
IntroParsing  
Introduction to parsing RDF into rdflib graphs

Reading an NT file

RDF data has various syntaxes (xml, n3, ntriples, trix, etc) that you might want to read. The simplest format is 'ntriples'. Create the file 'demo.nt' in the current directory with these two lines:

<http://bigasterisk.com/foaf.rdf#drewp> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
<http://bigasterisk.com/foaf.rdf#drewp> <http://example.com/says> "Hello world" .

In an interactive python interpreter, try this:

>>> from rdflib.Graph import Graph

>>> g = Graph()

>>> g.parse("demo.nt", format="nt")
<Graph identifier=QoYUuWMq0 (<class 'rdflib.Graph.Graph'>)>

>>> len(g)
2

>>> for stmt in g:
...     print stmt
... 
(rdflib.URIRef('http://bigasterisk.com/foaf.rdf#drewp'), rdflib.URIRef('http://example.com/says'), rdflib.Literal('Hello world', language=None, datatype=None))
(rdflib.URIRef('http://bigasterisk.com/foaf.rdf#drewp'), rdflib.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.URIRef('http://xmlns.com/foaf/0.1/Person'))

The final lines show how rdflib represents the two statements in the file. The statements themselves are just length-3 tuples; and the subjects, predicates, and objects are all rdflib types.

Reading remote graphs

Reading graphs from the net is just as easy:

>>> g.parse("http://bigasterisk.com/foaf.rdf")

>>> len(g)
42

The format defaults to 'xml', which is the common format for .rdf files you'll find on the net.

See also

the Graph.parse method

other parsers supported by rdflib


Sign in to add a comment
Hosted by Google Code