pythonrdflib

How to filter by language tag using g.triples in python rdflib?


I would like to create a dictionary where the keys are the subjects of the triples, and the values are the objects. I am querying a graph (in this example is g) where I have multiples labels, an dI would like to filter only on the english ones. How can I do this?

This is how I am creating the graph at the moment

d = {s: o for s, p, o in g.triples((None, SKOS.prefLabel, None))}

I would expect to add something like but I can't find the correct syntax.

d = {s: o for s, p, o in g.triples((None, SKOS.prefLabel, None), lang="en")}

Thanks!


Solution

  • You could do the filtering by the language tag in your dictionary comprehension like this:

    d = {s: o for s, p, o in g.triples((None, SKOS.prefLabel, None))
         if o.language == 'en'}