FOAF-parser??

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


On Sat, 21 Jun 2003 13:14:00 +0200, 
	Thomas Weholt <2002 at weholt.org> 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()
ts.parse_URI('http://www.amk.ca/amk.rdf')

# Get all subjects whose type is the FOAF Person class; returns a generator
gen = ts.subjects(const.TYPE, URIRef('http://xmlns.com/foaf/0.1/Person'))
# 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('http://xmlns.com/foaf/0.1/schoolHomepage'))

list(schools) is then ['http://www.dawsoncollege.qc.ca/',
'http://www.mcgill.ca'].

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.

--amk




More information about the Python-list mailing list