0

I am executing sparql query through python rdflib like

r=sparql.prepareQuery('SELECT ?label WHERE { <%s> rdfs:label ?label . }'%i)

My aim is to get the labels for the concepts in this way. As a result I get something like this:

rdflib.term.Literal(u'primary phloem sieve cell', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string'))

From this I want to extract the label and data type.(ie primary phloem sieve cell and string respectively for this case) I am using the

if type(o) == rdflib.term.Literal:
    output.append(o.toPython())

Where

o= rdflib.term.Literal(u'primary phloem sieve cell', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')) 

But it is not working. I am new to rdflib . Does anyone know how I can do that?

I know that I need to convert the result to string for label but what if the datatype is not string and I want to fetch the datatype

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
ankita singh
  • 115
  • 1
  • 1
  • 9

1 Answers1

3

I figured it out.As the type of the query result i.e o is 'rdflib.query.ResultRow' and o[0] is class 'rdflib.term.Literal'.Hence the if condition was not working. Removing the if condition worked for me and for the data type I need to execute another sparql query .

ankita singh
  • 115
  • 1
  • 1
  • 9