RDflib在windows7上导致打印错误

时间:2013-04-19 06:34:35

标签: debugging python-2.7 runtime-error sparql sparqlwrapper

我使用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"]

任何帮助?

1 个答案:

答案 0 :(得分:1)

return format不是JSON,因此setReturnFormat调用未按预期运行:

  

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

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

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