RDFLib in Python
Python provides several alternatives to parse RDF documents and ontologies. The most popular at the moment is RDFLib.
Installation
To store data we'll make use of SQLite3. In order to install it, open a terminal and type:
$ sudo apt-get install sqlite3
$ sudo apt-get install sqlite3
Use
In order to test if RDFLib is operating correctly in your computer, you can use the following code to extract data from the DBPedia entry and parse it.
from rdflib import Graph, URIRef
g = Graph()
g.parse("http://dbpedia.org/resource/Isaac_Asimov")
for stmt in g.subject_objects(URIRef("http://dbpedia.org/ontology/birthDate")):
print(str(stmt[1]))
from rdflib import Graph, URIRef
g = Graph()
g.parse("http://dbpedia.org/resource/Isaac_Asimov")
for stmt in g.subject_objects(URIRef("http://dbpedia.org/ontology/birthDate")):
print(str(stmt[1]))
|
|
Using custom namespaces
takiv2.py | |
File Size: | 0 kb |
File Type: | py |
References
[1] http://rdflib.readthedocs.io/en/stable/
[2] http://stackoverflow.com/questions/16829351/is-there-a-hello-world-example-for-sparql-with-rdflib
[2] http://stackoverflow.com/questions/16829351/is-there-a-hello-world-example-for-sparql-with-rdflib