from rdflib import Graph, Namespace, Literal # Define some namespaces rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#"); dc = Namespace("http://purl.org/dc/elements/1.1/") census = Namespace("tag:govshare.info,2005:rdf/census/") usgovt = Namespace("tag:govshare.info,2005:rdf/usgovt/"); # Create a store to hold RDF data store = Graph(); # Load the N3 file with the states data store.parse("states.n3", None, 'n3'); # Loop through every statement in the graph of the form # ( ____ rdf:type usgovt:State). totalpop = 0 statecount = 0 for statement in store.triples((None, rdf["type"], usgovt["State"])): state = statement[0] # the subject of the statement population = store.value(state, census["population"], None) population = int(population) # convert from Literal to integer totalpop = totalpop + population statecount = statecount + 1 print totalpop/statecount