4

我正在尝试在 Python (v3.3) 中使用 SPARQLWrapper 查询 DBpedia。这是我的查询:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?slot WHERE {
  <http://dbpedia.org/resource/Week> <http://www.w3.org/2002/07/owl#sameAs> ?slot
}

它会导致 SPARQLWrapper 包出错:

ValueError: Invalid \escape: line 118 column 74 (char 11126)

代码:

query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?slot WHERE{{ {subject} {predicate} {object} }} "

query = query.format(subject=subject, predicate=predicate, object= objectfield)
        self.sparql.setQuery(query)

        self.sparql.setReturnFormat(JSON)


    results = self.sparql.query().convert() # Error thrown at this line 

错误 :

Traceback (most recent call last):
  File "getUriLiteralAgainstPredicate.py", line 84, in <module>
    sys.exit(main())
  File "getUriLiteralAgainstPredicate.py", line 61, in main
    entity,predicateURI,result = p.getObject(dataAtURI,predicates, each["entity"])
  File "getUriLiteralAgainstPredicate.py", line 30, in getObject
    result = self.run_sparql("<"+subjectURI+">","<"+predicateURI+">","?slot")
  File "getUriLiteralAgainstPredicate.py", line 24, in run_sparql
    results = self.sparql.query().convert()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/Wrapper.py", line 539, in convert
    return self._convertJSON()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/Wrapper.py", line 476, in _convertJSON
    return jsonlayer.decode(self.response.read().decode("utf-8"))
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/jsonlayer.py", line 76, in decode
    return _decode(string)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/jsonlayer.py", line 147, in <lambda>
    _decode = lambda string, loads=json.loads: loads(string)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 118 column 74 (char 11126)
4

2 回答 2

5

问题是,dbpedia 输出有这一行:

{ "slot": { "type": "uri", "value": "http://got.dbpedia.org/resource/\U00010345\U00010339\U0001033A\U00010349" }},

\U注意以(大写 U)开头的文字。这不是有效的 JSON,python 不知道如何处理它。因此,问题出在 DBPedia 端,无法在 SPARQLWrapper 端处理。

但是……你可以像这样自己处理:

results = self.sparql.query()
body = results.response.read()

fixed_body = body.decode("unicode_escape")

from SPARQLWrapper.Wrapper import jsonlayer
results = jsonlayer.decode(fixed_body)
于 2013-12-06T11:13:21.683 回答
4

尝试python-cjson

所以上面的事情也可以尝试如下

import cjson
results = self.sparql.query()
body = results.response.read()
results = cjson.decode(body)
于 2014-08-14T14:48:33.163 回答