A.M. Kuchling amk at
Mon Jun 23 15:47:23 CEST 2003

On Sat, 21 Jun 2003 13:14:00 +0200, 
	Thomas Weholt <2002 at> wrote:
> Tramp[2] and RDFLib, but they don't seem to fit the task somehow. All I want
> is to parse a FOAF-file and get a list of persons back, each person may
> again have a list of persons he/she knows etc. My attempts so far has been a
> terrible mess of SAX, pulldom and rdflib.

You shouldn't need to mess with SAX and pulldom at all; rdflib will
take care of the parsing for you.  For example:

from rdflib.triple_store import TripleStore
from rdflib.nodes import URIRef
from rdflib import const

ts = TripleStore()

# Get all subjects whose type is the FOAF Person class; returns a generator
gen = ts.subjects(const.TYPE, URIRef(''))
# Convert generator to a list
L = list(gen)

I don't list anyone I know in my FOAF file, but finding my school affiliations
is a similar task:

amk = L[0]
schools = ts.objects(amk, URIRef(''))

list(schools) is then ['',

The current CVS version of RDFLib has rearranged things a bit, and I forget
if I'm running the CVS or the last stable version, so the code above may
need some tweaking.  Still, figuring out the information you need shouldn't
be a very difficult task, whatever version of rdflib you're trying to use.


More information about the Python-list mailing list