RDFLib
From GetSemantic
RDFLib is an RDF library written in the Python programming language.
Contents |
[edit] Documentation
[edit] Loading a graph
import rdflib
graph = rdflib.ConjunctiveGraph()
graph.parse("test.rdf")
See ConjunctiveGraph and ConjunctiveGraph.parse()
[edit] Iterating through the graph
Following on from previous sample:
for s, p, o in graph:
print "Subject = " + s
print "Predicate = " + p
print "Object = " + o
See Graph.__iter__
[edit] Querying a graph using SPARQL
Following on from previous samples:
results = graph.query("PREFIX foaf: <http://xmlns.com/foaf/0.1/> \
SELECT ?friend ?name WHERE { <http://tommorris.org/foaf#me> foaf:knows ?friend . ?friend foaf:name ?name . }")
for i in results.selected:
print str(i[1]) # prints friends name, since 'name' is the second variable in the SELECT
See Graph.query()
Currently RDFLib supports only SELECT and ASK queries, although CONSTRUCT and DESCRIBE queries are slated for future versions.
[edit] Adding a statement to a graph
import rdflib
graph = rdflib.ConjunctiveQuery()
graph.add((rdflib.URIRef('http://example.org/subject'), rdflib.URIRef('http://example.org/predicate'), rdflib.Literal('Object!')))
See Graph.add()
[edit] Serializing graph
RDFLib supports a number of different serialization formats using the Graph.serialize() method. The 'format' argument specifies the format:
- 'xml' returns RDF/XML
- 'n3' returns Notation3
- 'nt' returns N-Triples
- 'turtle' returns Turtle
- 'pretty-xml' returns Pretty Printed RDF/XML
[edit] See Also
- Homepage
- Documentation
- Projects
- Wikipedia article
- IRC: irc.freenode.net #redfoot
- Building Metadata Applications with RDF by Bob DuCharme

