我正在使用rdflib来查询一系列rdf文件,其中我知道名称有一个语言标记(当我在sparql端点查询时),这是我的数据的快照:
但是当我在python中解析rdf文件并提取' name'价值我只得到"约瑟夫斯"没有语言标签。
我可以单独使用以下内容获取标记:
bind(lang(?name) as ?lan)
但这并不是。我序列化图表时需要有名称和标签。
任何可能导致此信息丢失的建议以及我如何使用其语言标记获取我的名字?
这是一个非常简单的查询。这是我的完整脚本:
import unicodedata
from rdflib import Namespace, URIRef, Graph , Literal , OWL, RDFS , RDF
from SPARQLWrapper import SPARQLWrapper2, XML , JSON , TURTLE
import os
sparql = SPARQLWrapper2("http://dbpedia.org/sparql")
os.chdir('...\Desktop')
jl = Namespace("http://data.judaicalink.org/ontology/")
foaf = Namespace("http://xmlns.com/foaf/0.1/")
graph = Graph()
spar= ("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?occ ?x ?name ?same
where {
<http://dbpedia.org/resource/Category:Jewish_historians> rdfs:label ?occ.
?x dct:subject <http://dbpedia.org/resource/Category:Jewish_historians>.
?x rdfs:label ?name.
?x owl:sameAs ?same.
}
""")
sparql.setQuery(spar)
sparql.setReturnFormat(TURTLE)
results = sparql.query().convert()
graph.bind('jl', jl)
graph.bind('foaf',foaf)
if (u"x",u"name",u"occ",u"same") in results:
bindings = results[u"x",u"name",u"occ",u"same"]
for b in bindings:
graph.add( (URIRef(b[u"x"].value), RDF.type , foaf.Person ) )
graph.add( (URIRef(b[u"x"].value), jl.hasLabel, Literal(b[u"name"].value) ) )
graph.serialize(destination= 'output.rdf' , format="turtle")