本文整理匯總了Python中rdflib.Graph.ConjunctiveGraph.predicate_objects方法的典型用法代碼示例。如果您正苦於以下問題:Python ConjunctiveGraph.predicate_objects方法的具體用法?Python ConjunctiveGraph.predicate_objects怎麽用?Python ConjunctiveGraph.predicate_objects使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rdflib.Graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.predicate_objects方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RdfParser
# 需要導入模塊: from rdflib.Graph import ConjunctiveGraph [as 別名]
# 或者: from rdflib.Graph.ConjunctiveGraph import predicate_objects [as 別名]
class RdfParser(object):
"""A basic wrapper for RdfLib's RDF parser.
This class aims to accomplish easier parsing, extraction of Models,
etc."""
def __init__(self, rdf, format='guess'):
"""Init the parser with the graph string."""
self.graph = Graph()
if format == 'guess':
format = self.__guess_format(rdf)
print 'RdfParser guesses format to be: %s' % format
try:
self.graph.load(StringIO(rdf), format=format)
except:
print "Failed to parse RDF:"
print rdf[0:100]
def extract(self, datatype):
"""Extract all of the data of a given datatype."""
data = []
ns = RdfSerializer.NAMESPACES['sylph'] # TODO: Awkward.
for sub in self.graph.subjects(RDF.type, ns[datatype]):
idx = str(sub)
item = {'uri': idx}
for pred, obj in self.graph.predicate_objects(sub):
if pred == RDF.type:
continue
if obj == ns['None']:
obj = None
elif type(obj) == URIRef:
obj = unicode(obj)
elif type(obj) == Literal:
obj = obj.toPython()
if type(obj) == Literal: # Don't be silly, RdfLib!
obj = unicode(obj)
predstr = str(pred).rpartition('#')[2].rpartition('/')[2]
item[predstr] = obj
data.append(item)
return data
@staticmethod
def __guess_format(st):
"""Guess the format of the input string."""
# TODO: At present, it can only guess between XML and n3, even
# then this is a vague heuristic.
if st.startswith('<'):
return 'xml'
return 'n3'