RDFLIB

45 views
Skip to first unread message

Nisarg Patel

unread,
Jun 7, 2020, 3:44:27 PM6/7/20
to rdflib-dev
import rdflib

g = rdflib.Graph()

g.parse("ExampleRdf1.rdf")  #I have uploaded RDF file.

qres = g.query(
    """
    select ?i ?type where {
  ?i a/(rdfs:subClassOf)* ?type
}""")
    

for row in qres:
    print(" %s is %s" % row  )
    

##output of this query:

 
 
 I want to remove this link(http://stackoverflow.com/q/20474862/1281433) from result. its Prefix.
 i need output like this:
 
 Apurva is Man
 Apurva is Mortal
 Cathy is Girl
 Cathy is Mad.
 
 
 Can anyone help me with this?


#ExampleRdf1.rdf file.

<?xml version="1.0" encoding="utf-8" ?>
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

    <rdf:type>
      <rdf:Description rdf:about="http://stackoverflow.com/q/20474862/1281433/Man">
        <rdfs:subClassOf rdf:resource="http://stackoverflow.com/q/20474862/1281433/Mortal"/>
      </rdf:Description>
    </rdf:type>

  </rdf:Description>

  <rdf:Description rdf:about="http://stackoverflow.com/q/20474862/1281433/Cathy">
    <rdf:type>
      <rdf:Description rdf:about="http://stackoverflow.com/q/20474862/1281433/Girl">
        <rdfs:subClassOf rdf:resource="http://stackoverflow.com/q/20474862/1281433/Mad"/>
      </rdf:Description>
    </rdf:type>

  </rdf:Description>

</rdf:RDF>

Boris Pelakh

unread,
Jun 8, 2020, 10:50:42 AM6/8/20
to rdflib-dev
Easiest way is to strip the namespaces yourself:

import re

for row in qres:
   
print(" %s is %s" % tuple(re.sub(r'^.*[/#]', '', r) for r in row))

If you want to use the namespace manager, you can do this similarly:

for row in qres:
   
print(" %s is %s" % tuple(g.qname(r).split(':')[1] for r in row))



Donny Winston

unread,
Jun 8, 2020, 11:18:30 AM6/8/20
to rdflib-dev
Ideally, you'd associate a rdfs:label or something with each URI resource and print the labels instead, but you can also bind a namespace and format output according to that. In your case, perhaps

```python
import rdflib

g
= rdflib.Graph()

g
.parse("ex.rdf")
g
.bind("so", "http://stackoverflow.com/q/20474862/1281433/")

qres
= g.query(
   
"""
    select ?i ?type where {
  ?i a/(rdfs:subClassOf)* ?type
}"""
)
for row in qres:

    i
, type_ = [e.n3(g.namespace_manager) for e in row]
   
print(f"{i} is {type_}")


```
yields
```
so:Apurva is so:Man
so:Apurva is so:Mortal
so:Cathy is so:Girl
so:Cathy is so:Mad
```

Wes Turner

unread,
Jun 8, 2020, 7:10:06 PM6/8/20
to rdfli...@googlegroups.com
FWIW, in Python 3.9, there are new str.removeprefix() and str.removesuffix() methods; though you'd still be doing two containment tests whereas re.sub only does only containment test (but does require an 'import re')

--
http://github.com/RDFLib
---
You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/0b0cd799-73d5-40e2-961a-c02ca8ad896eo%40googlegroups.com.

Nicholas Car

unread,
Jun 18, 2020, 7:43:13 PM6/18/20
to rdfli...@googlegroups.com
Hi all,

Let’s keep these “how to” question on stackoverflow, not here in the dev mailing list.

Nick

— 
Dr Nicholas Car
Data Systems Architect
SURROUND Australia
0477 560 177

On 9 Jun 2020, at 9:10 am, Wes Turner <wes.t...@gmail.com> wrote:


Reply all
Reply to author
Forward
0 new messages