Package: rdflib
RDFLibRDFLib is a pure Python package for working withRDF. RDFLib contains most things you need to work with RDF, including:parsers and serializers for RDF/XML, N3, NTriples, N-Quads, Turtle, TriX, Trig and JSON-LDa Graph interface which can be backed by any one of a number of Store implementationsstore implementations for in-memory, persistent on disk (Berkeley DB) and remote SPARQL endpointsa SPARQL 1.1 implementation - supporting SPARQL 1.1 Queries and Update statementsSPARQL function extension mechanismsRDFlib Family of packagesThe RDFlib community maintains many RDF-related Python code repositories with different purposes. For example:rdflib- the RDFLib coresparqlwrapper- a simple Python wrapper around a SPARQL service to remotely execute your queriespyLODE- An OWL ontology documentation tool using Python and templating, based on LODE.pyrdfa3- RDFa 1.1 distiller/parser library: can extract RDFa 1.1/1.0 from (X)HTML, SVG, or XML in general.pymicrodata- A module to extract RDF from an HTML5 page annotated with microdata.pySHACL- A pure Python module which allows for the validation of RDF graphs against SHACL graphs.OWL-RL- A simple implementation of the OWL2 RL Profile which expands the graph with all possible triples that OWL RL defines.Please see the list for all packages/repositories here:https://github.com/RDFLibHelp with maintenance of all of the RDFLib family of packages is always welcome and appreciated.Versions & Releases7.1.0a0currentmainbranch.7.x.ycurrent release, supports Python 3.8.1+ only.seeReleases6.x.ysupports Python 3.7+ only. Many improvements over 5.0.0seeReleases5.x.ysupports Python 2.7 and 3.4+ and ismostly backwards compatible with 4.2.2.Seehttps://rdflib.devfor the release overview.DocumentationSeehttps://rdflib.readthedocs.iofor our documentation built from the code. Note that there arelatest,stable5.0.0and4.2.2documentation versions, matching releases.InstallationThe stable release of RDFLib may be installed with Python's package management toolpip:$ pip install rdflibAlternatively manually download the package from the Python Package
Index (PyPI) athttps://pypi.python.org/pypi/rdflibThe current version of RDFLib is 7.0.0, see theCHANGELOG.mdfile for what's new in this release.Installation of the current main branch (for developers)Withpipyou can also install rdflib from the git repository with one of the following options:$ pip install git+https://github.com/rdflib/rdflib@mainor$ pip install -e git+https://github.com/rdflib/rdflib@main#egg=rdflibor from your locally cloned repository you can install it with one of the following options:$ poetry install  # installs into a poetry-managed venvor$ pip install -e .Getting StartedRDFLib aims to be a pythonic RDF API. RDFLib's main data object is aGraphwhich is a Python collection
of RDFSubject, Predicate, ObjectTriples:To create graph and load it with RDF data from DBPedia then print the results:fromrdflibimportGraphg=Graph()g.parse('http://dbpedia.org/resource/Semantic_Web')fors,p,oing:print(s,p,o)The components of the triples are URIs (resources) or Literals
(values).URIs are grouped together bynamespace, common namespaces are included in RDFLib:fromrdflib.namespaceimportDC,DCTERMS,DOAP,FOAF,SKOS,OWL,RDF,RDFS,VOID,XMLNS,XSDYou can use them like this:fromrdflibimportGraph,URIRef,Literalfromrdflib.namespaceimportRDFS,XSDg=Graph()semweb=URIRef('http://dbpedia.org/resource/Semantic_Web')type=g.value(semweb,RDFS.label)WhereRDFSis the RDFS namespace,XSDthe XML Schema Datatypes namespace andg.valuereturns an object of the triple-pattern given (or an arbitrary one if multiple exist).Or like this, adding a triple to a graphg:g.add((URIRef("http://example.com/person/nick"),FOAF.givenName,Literal("Nick",datatype=XSD.string)))The triple (in n-triples notation)<http://example.com/person/nick> <http://xmlns.com/foaf/0.1/givenName> "Nick"^^<http://www.w3.org/2001/XMLSchema#string> .is created where the propertyFOAF.givenNameis the URI<http://xmlns.com/foaf/0.1/givenName>andXSD.stringis the
URI<http://www.w3.org/2001/XMLSchema#string>.You can bind namespaces to prefixes to shorten the URIs for RDF/XML, Turtle, N3, TriG, TriX & JSON-LD serializations:g.bind("foaf",FOAF)g.bind("xsd",XSD)This will allow the n-triples triple above to be serialised like this:print(g.serialize(format="turtle"))With these results:PREFIXfoaf:<http://xmlns.com/foaf/0.1/>PREFIXxsd:<http://www.w3.org/2001/XMLSchema#><http://example.com/person/nick>foaf:givenName"Nick"^^xsd:string.New Namespaces can also be defined:dbpedia=Namespace('http://dbpedia.org/ontology/')abstracts=list(xforxing.objects(semweb,dbpedia['abstract'])ifx.language=='en')See also./examplesFeaturesThe library contains parsers and serializers for RDF/XML, N3,
NTriples, N-Quads, Turtle, TriX, JSON-LD, RDFa and Microdata.The library presents a Graph interface which can be backed by
any one of a number of Store implementations.This core RDFLib package includes store implementations for
in-memory storage and persistent storage on top of the Berkeley DB.A SPARQL 1.1 implementation is included - supporting SPARQL 1.1 Queries and Update statements.RDFLib is open source and is maintained onGitHub. RDFLib releases, current and previous
are listed onPyPIMultiple other projects are contained within the RDFlib "family", seehttps://github.com/RDFLib/.Running testsRunning the tests on the hostRun the test suite withpytest.poetryinstall
poetryrunpytestRunning test coverage on the host with coverage reportRun the test suite and generate a HTML coverage report withpytestandpytest-cov.poetryrunpytest--covViewing test coverageOnce tests have produced HTML output of the coverage report, view it by running:poetryrunpytest--cov--cov-reportterm--cov-reporthtml
python-mhttp.server--directory=htmlcovContributingRDFLib survives and grows via user contributions!
Please read ourcontributing guideanddevelopers guideto get started.
Please consider lodging Pull Requests here:https://github.com/RDFLib/rdflib/pullsTo get a development environment consider using Gitpod or Google Cloud Shell.You can also raise issues here:https://github.com/RDFLib/rdflib/issuesSupport & ContactsFor general "how do I..." queries, please usehttps://stackoverflow.comand tag your question withrdflib.
Existing questions:https://stackoverflow.com/questions/tagged/rdflibIf you want to contact the rdflib maintainers, please do so via:the rdflib-dev mailing list:https://groups.google.com/group/rdflib-devthe chat, which is available atgitteror via matrix#RDFLib_rdflib:gitter.im