1. pragma section-numbers off

Here's some Python code using PythonInfo:RdfLib that you can use to start getting used to reading FOAF files from Python.

It also demonstrates some of the power of Schema, because it loads TheFoafSchema to automatically translate things like
into readable "
sha1sum of a personal mailbox URI name
." Wow!

This code's all free, Public Domain. I wrote it. Do what you like with it.

-- LionKimbro DateTime(2004-06-06T08:54:49Z)

A demonstration of using RDFLib to read FOAF files.

Not only will we read the file, but we'll use the
FOAF schema to print it humanely.

FOAF triples, drawn raw, look like this:

  _:Chacbxmz1 _:Chacbxmz10
    (subject)   (property)                      (object)

...meaning that a person represented by a blank node ("bNode")
   knows another person, represented by another blank node.

Instead, we want to draw this:

  Lion Kimbro | knows | Alex Schroeder

We learn the names of the people represented by the blank
nodes by looking up the "foaf:name" property.


  (_Chacbxmz1) "Lion Kimbro"
  (blank node   (property)                    (object)

...abbreviated to just:

  (blank node)  foaf:name   "Lion Kimbro"
  (subject)     (property)  (object)

We'll store all the (blank node)-->(name) mappings
in a dictionary.

Then we could output:

  Lion Kimbro  Alex Schroeder
  (subject's   (property)                       (object's
   name)                                          name)

...but that'd look ugly.

Wouldn't it be nice if we could change all the property URL's
into nice human readable names?

In fact, we can do just that.

There are things called "RDF Schema," which describe
an RDF language in human terms.

It draws things like this:

  (object)   "knows"

How conveniente!

So, first we'll read the schema, build a mapping from
full RDF property URL's, to their nice convenient names,
and then we'll use that mapping to print out our whole

In total, our program will:

  (1) Read the FOAF schema, to learn the names of properties.
  (2) Read a FOAF file.
  (3) Figure out the names of all the blank nodes.
  (4) Print out the FOAF triples having to do with People.

import rdflib
from rdflib.TripleStore import TripleStore
import pprint

# Replace this with your own FOAF file URL:
foaf_file_to_read = ""

# Machine readable FOAF schema information.
# Human readable version:
# (check it out.)
foaf_schema_url = ""

# These are properties, as in (subject) (property) (object).
# Hard coded property URLs:
rdfs_label = "" # rdfs:label
foaf_name = "" # foaf:name

# (1) Load FOAF schema information...
schema_store = TripleStore()
schema_store.load( foaf_schema_url )

# ...and learn the English names of FOAF properties.
foaf_property_label = {}
for a,b,c in schema_store:
    if b==rdfs_label:
        foaf_property_label[a] = c

# Uncomment this if you want to see what the dictionary looks like.
#pprint.pprint( property_label )

# (2) Read a FOAF file.
foaf_store = TripleStore()
foaf_store.load( foaf_file_to_read ) # something like ""

# (3) Figure out the names of all the blank nodes.
# i.e., We look through all the triples, and find the ones of the form:
#    (bnode) foaf:name (string)
person_bnode_name = {}
for a,b,c in foaf_store:
    if b == foaf_name:
        person_bnode_name[a]=c # found one! keep track of it.

# (4) Print out the interesting information about People.
for a,b,c in foaf_store:
    # Is this describing someone we know?
    # Do we know this property?
    if a in person_bnode_name and b in foaf_property_label:
        # If the object is someone we know, let's recall their name, too.
        if c in person_bnode_name:
        # Print it out!
        print person_bnode_name[a] + " | " + foaf_property_label[b] + " | " + c