Masoom Raza
Masoom Raza

Reputation: 179

How to add rdflib plugin (rdflib.serializer.Serializer) in python (jupyter notebook)

I am working with RDF and using rdflib in python. Able to read, subject predicate and object but when tried to view in turtle format its giving plugin error.

#pip install rdflib
#pip show rdflib

from rdflib import Graph

# Intializing Graph
g = Graph()

# Parse in a RDF file graph dbpedia
g.parse('http://dbpedia.org/resource/Michael_Jackson')

# Loop through  each triplet in graph (subj, pred, obj)
for index, (sub, pred, obj) in enumerate(g):
    print(sub, pred, obj)
    if index == 10:
        break

#print entire graph in RDF turtle format
print(g.serialize(format='tt1').decode('u8'))

For the last command I am getting error as :

---------------------------------------------------------------------------

PluginException: No plugin registered for (tt1, <class 'rdflib.serializer.Serializer'>)

Upvotes: 0

Views: 595

Answers (2)

Nicholas Car
Nicholas Car

Reputation: 1251

@masoom-raza: if this issue is solved (by you!) can you please mark it closed? This is to ensure that all [rdflib] issues that are listed in StackOverflow are kept up-to-date. Thanks.

Also, you might like to:

  1. use more conventional looping in the Graph object with triples()
  2. Use RDFlib 6.0.0a0 (master in the repo, no the pip version) so you don't have to use serialize().decode() or even format=ttl as that's defauly
from rdflib import Graph

# Intializing Graph
g = Graph()

# Parse in a RDF file graph dbpedia
g.parse('http://dbpedia.org/resource/Michael_Jackson')

# Loop through  each triplet in graph (subj, pred, obj) but no limit
for sub, pred, obj in g.triples():
    print(sub, pred, obj)
   

# print entire graph in RDF turtle format
print(g.serialize())

Upvotes: 2

Masoom Raza
Masoom Raza

Reputation: 179

There is the small mistake, format=('tt1'). Instead we have to use format=('ttl'). Alphabet l instead of numeric 1.

Upvotes: 1

Related Questions