pyparsing.ParseException:预期{SelectQuery |构造查询 |描述查询 |问询}
pyparsing.ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery}
我在我的代码中 运行 以下 sparql,但出现错误。
def get_parents_of_node(node):
sparql = """select ?superclass where {{
:{0} rdfs:subClassOf* ?superclass
}}""".format(node)
q = g.query(sparql)
for row in q:
print row
get_parents_of_node('http://cccc.com#LaptopHighPerformance')
错误信息是
Traceback (most recent call last):
File "C:\Code\Python\RDF Parser\RDF_Parser.py", line 244, in <module>
get_parents_of_node('http://cccc.com#LaptopHighPerformance')
File "C:\Code\Python\RDF Parser\RDF_Parser.py", line 68, in get_parents_of_node
q = g.query(sparql)
File "C:\Python27\lib\site-packages\rdflib\graph.py", line 1085, in query
query_object, initBindings, initNs, **kwargs))
File "C:\Python27\lib\site-packages\rdflib\plugins\sparql\processor.py", line 74, in query
parsetree = parseQuery(strOrQuery)
File "C:\Python27\lib\site-packages\rdflib\plugins\sparql\parser.py", line 1058, in parseQuery
return Query.parseString(q, parseAll=True)
File "C:\Python27\lib\site-packages\pyparsing.py", line 1632, in parseString
raise exc
pyparsing.ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery} (at char 36), (line:2, col:9)
[Finished in 3.8s with exit code 1]
[shell_cmd: python -u "C:\Code\Python\RDF Parser\RDF_Parser.py"]
正在阅读documentation, you are supposed to pass values (with the appropriate type) in using the initBindings
argument to map the desired query variable to a specific value when calling query
。例如:
def get_parents_of_node(node):
sparql = """select ?superclass where {{
?class rdfs:subClassOf* ?superclass
}}"""
q = g.query(sparql, initBindings={'class': rdflib.URIRef(node)})
for row in q:
print row
这当然假设节点确实是一个 URI 引用。
我在我的代码中 运行 以下 sparql,但出现错误。
def get_parents_of_node(node):
sparql = """select ?superclass where {{
:{0} rdfs:subClassOf* ?superclass
}}""".format(node)
q = g.query(sparql)
for row in q:
print row
get_parents_of_node('http://cccc.com#LaptopHighPerformance')
错误信息是
Traceback (most recent call last):
File "C:\Code\Python\RDF Parser\RDF_Parser.py", line 244, in <module>
get_parents_of_node('http://cccc.com#LaptopHighPerformance')
File "C:\Code\Python\RDF Parser\RDF_Parser.py", line 68, in get_parents_of_node
q = g.query(sparql)
File "C:\Python27\lib\site-packages\rdflib\graph.py", line 1085, in query
query_object, initBindings, initNs, **kwargs))
File "C:\Python27\lib\site-packages\rdflib\plugins\sparql\processor.py", line 74, in query
parsetree = parseQuery(strOrQuery)
File "C:\Python27\lib\site-packages\rdflib\plugins\sparql\parser.py", line 1058, in parseQuery
return Query.parseString(q, parseAll=True)
File "C:\Python27\lib\site-packages\pyparsing.py", line 1632, in parseString
raise exc
pyparsing.ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery} (at char 36), (line:2, col:9)
[Finished in 3.8s with exit code 1]
[shell_cmd: python -u "C:\Code\Python\RDF Parser\RDF_Parser.py"]
正在阅读documentation, you are supposed to pass values (with the appropriate type) in using the initBindings
argument to map the desired query variable to a specific value when calling query
。例如:
def get_parents_of_node(node):
sparql = """select ?superclass where {{
?class rdfs:subClassOf* ?superclass
}}"""
q = g.query(sparql, initBindings={'class': rdflib.URIRef(node)})
for row in q:
print row
这当然假设节点确实是一个 URI 引用。