我有一个rdf文件,例如:
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dbp="http://dbpedia.org/ontology/"
xmlns:dbprop="http://dbpedia.org/property/"
xmlns:foaf="http://xmlns.com/foaf/0.1/">
<rdf:Description rdf:about="http://dbpedia.org/page/Johann_Sebastian_Bach">
<dbp:birthDate>1685-03-21</dbp:birthDate>
<dbp:deathDate>1750-07-28</dbp:deathDate>
<dbp:birthPlace>Eisenach</dbp:birthPlace>
<dbp:deathPlace>Leipzig</dbp:deathPlace>
<dbprop:shortDescription>German composer and organist</dbprop:shortDescription>
<foaf:name>Johann Sebastian Bach</foaf:name>
<rdf:type rdf:resource="http://dbpedia.org/class/yago/GermanComposers"/>
<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
</rdf:Description>
</rdf:RDF>
我只想提取这个文件的文本部分,在这种情况下,我的输出是:
output_ tex = "Johann Sebastian Bach, German composer and organist,1685-03-21, 1750-07-28, Eisenach, Leipzig"
如何使用RDFlib获得此结果?
最佳答案
Building on Joshua Taylor's answer, the method you are looking for is "toPython" which the docs say " Returns an appropriate python datatype derived from this RDF Literal
". 此片段应返回您要查找的内容:
raw_data = """<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dbp="http://dbpedia.org/ontology/"
xmlns:dbprop="http://dbpedia.org/property/"
xmlns:foaf="http://xmlns.com/foaf/0.1/">
<rdf:Description rdf:about="http://dbpedia.org/page/Johann_Sebastian_Bach">
<dbp:birthDate>1685-03-21</dbp:birthDate>
<dbp:deathDate>1750-07-28</dbp:deathDate>
<dbp:birthPlace>Eisenach</dbp:birthPlace>
<dbp:deathPlace>Leipzig</dbp:deathPlace>
<dbprop:shortDescription>German composer and organist</dbprop:shortDescription>
<foaf:name>Johann Sebastian Bach</foaf:name>
<rdf:type rdf:resource="http://dbpedia.org/class/yago/GermanComposers"/>
<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
</rdf:Description>
</rdf:RDF>"""
import rdflib
graph = rdflib.Graph()
graph.parse(data=raw_data)
output = []
for s, p, o in graph:
if type(o) == rdflib.term.Literal:
output.append(o.toPython())
print ', '.join(output)