from rdflib import Namespace >>> SDO = Namespace("https://schema.org/") >>> SDO.Person https://schema.org/Person >>> SDO['url'] https://schema.org/url Graphs as iterators RDFLib graphs also override __iter__ in order to support iteration over the contained triples: for subject, predicate, object_ in someGraph: assert (subject, predicate, object_) in someGraph, "Iterator / Container Protocols are Broken!!" Set operations on RDFLib graphs __iadd__ and __isub__ are overridden to support adding and subtracting Graphs to/from each other (in place): G1 += G1 G2 -= G2 Basic triple matching RDFLib graphs support basic triple pattern matching with a triples((subject,predicate,object)) function. This function is a generator of triples that match the pattern given by the arguments. The arguments of these are RDF terms that restrict the triples that are returned. Terms that are None are treated as a wildcard. for subject, predicate, object_ in someGraph.triples((None, URIRef("https://schema.org/name"), None)): print("{} has name {}".format(s, o)) # prints all the triples with the predicate being https://schema.org/name RDF convenience APIs (RDF collections / containers) Managing triples Adding triples Triples can be added in two ways: They may be added with the parse(source, publicID=None, format="xml") function. The first argument can be a source of many kinds, but the most common is the serialization (in various formats: RDF/XML, Notation 3, N-Triples of an RDF graph as a string). The format parameter is one of turtle, n3, xml, n-triples or JSON-LD (this last when the JSON-LD plugin is used). publicID is the name of the graph into which the RDF serialization will be parsed. Triples can also be added with the add function: add((subject, predicate, object)). Removing triples Similarly, triples can be removed by a call to remove: remove((subject, predicate, object)) RDF Literal support RDFLib 'Literal's essentially behave like Unicode characters with an XML Schema datatype or language attribute. The class provides a mechanism to both convert Python literals (and their built-ins such as time/date/datetime) into equivalent RDF Literals and (conversely) convert Literals to their Python equivalent. There is some support of considering datatypes in comparing Literal instances, implemented as an override to __eq__. This mapping to and from Python literals is achieved with the following dictionaries: PythonToXSD = { basestring : (None, None), float : (None, XSD_NS+u'float'), int : (None, XSD_NS+u'int'), long : (None, XSD_NS+u'long'), bool : (None, XSD_NS+u'boolean'), date : (lambda i:i.isoformat(), XSD_NS+u'date'), time : (lambda i:i.isoformat(), XSD_NS+u'time'), datetime : (lambda i:i.isoformat(), XSD_NS+u'dateTime'), } Maps Python instances to WXS datatyped Literals XSDToPython = { XSD_NS+u'time' : (None,