0

我使用 SPARQLWrapper 创建 SPARQL 查询,但我不知道如何调试以下错误消息:

Warning (from warnings module):
  File "D:\Python27\lib\site-packages\sparqlwrapper-1.5.2-py2.7.egg\SPARQLWrapper\Wrapper.py", line 550
RuntimeWarning: unknown response content type, returning raw response...

Traceback (most recent call last):
  File "D:\Python27\testwrapper.py", line 31, in <module>
    if (len(results["results"]["bindings"]) == 0):
AttributeError: addinfourl instance has no attribute '__getitem__'

这是我的代码:

from SPARQLWrapper import SPARQLWrapper,JSON
sparql = SPARQLWrapper('http://thedatahub.org/dataset/semanticquran');

queryString = """
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  olia-ar: <http://purl.org/olia/arabic_khoja.owl#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  lexvo: <http://lexvo.org/id/iso639-3/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  gold: <http://purl.org/linguistics/gold/>
PREFIX  skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX  qvoc: <http://www.nlp2rdf.org/quranvocab#>
SELECT DISTINCT  ?wordText ?pos
WHERE
 { ?wordPart rdf:type qvoc:LexicalItem .
    ?wordPart gold:Root "smw" .
    ?wordPart dcterms:isPartOf ?word .
    ?wordPart gold:PartOfSpeechProperty ?pos .
    ?word rdf:type qvoc:Word .
    ?word skos:prefLabel ?wordText
  }
"""

sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
if (len(results["results"]["bindings"]) == 0):
  print "No results found."
else:
  for result in results["results"]["bindings"]:
     print result["wordText"]["value"]

有什么帮助吗?

4

1 回答 1

1

返回格式不是 JSON,因此调用setReturnFormat未按预期工作:

根据返回格式对返回值进行编码:

  • 在 XML 的情况下,返回一个 DOM 顶部元素;
  • 在 JSON 的情况下,simplejson 转换将返回一个字典;
  • 对于 RDF/XML,值通过 RDFLib 转换为 Graph 实例。

在所有其他情况下,输入只是返回。

于 2013-10-21T17:49:03.710 回答