我正在使用 rdflib 来查询一系列 rdf 文件,其中我知道名称有一个语言标记(当我在 sparql 端点查询时),这是我的数据的快照:

样本数据

但是当我在 python 中解析 rdf 文件并提取'name'值时,我只得到没有语言标记的“Josephus”。

我可以单独获取标签,例如:

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")