我编写了一个Python脚本,该脚本应该能够运行dbpedia URI列表并对它们运行查询。但是,出于某种原因,我在
上收到错误qres = g.query(query)
当我运行此代码时。有谁知道为什么会发生这种情况以及如何解决这个问题?我真的陷入了困境,而且我在论文时间表上落后了,所以压力确实在增加。
代码:
import rdflib
import csv
import pandas as pd
colnames = ['Link']
list2 = pd.read_csv('C:/Users/Frank/Google Drive/Master Scriptie/testtest3.csv', sep=',', header=None, usecols=[2], names=colnames)
saved_column = list2.Link
outputfile = open('C:/Users/Frank/Google Drive/Master Scriptie/code files/dbpedia_output/test_dataset_uri_subject.csv', 'w')
reader = csv.reader(saved_column)
g = rdflib.Graph()
for uri in reader:
uri2 = "".join(str(x) for x in uri)
uri2 = uri2[1:].rstrip()
print (uri2)
result = g.parse("http://dbpedia.org" + uri2)
print (result)
query = "SELECT ?subject WHERE {<http://dbpedia.org" + uri2 + "> dbo:wikiPageRedirects*/dct:subject ?subject .}"
print ("query: " + query)
qres = g.query(query)
for singlerow in qres:
subject_final = "%s" % singlerow
outputfile.write("{0}, {1} \n".format(uri,subject_final)
cmd中的错误消息:
/resource/Sheldon_J._Plankton
[a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory']].
query: SELECT ?subject WHERE {<http://dbpedia.org/resource/Sheldon_J._Plankton>
dbo:wikiPageRedirects*/dct:subject ?subject .}
Traceback (most recent call last):
File "rdfimport.py", line 47, in <module>
qres = g.query(query)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\graph.py", line 1089, in query
query_object, initBindings, initNs, **kwargs))
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\processor.py", line 75, in query
query = translateQuery(parsetree, base, initNs)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 764, in translateQuery
q[1], visitPost=functools.partial(translatePName, prologue=prologue))
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 384, in traverse
r = _traverse(tree, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 345, in _traverse
e[k] = _traverse(val, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 345, in _traverse
e[k] = _traverse(val, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in _traverse
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in <listcomp>
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 345, in _traverse
e[k] = _traverse(val, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in _traverse
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in <listcomp>
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in _traverse
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in <listcomp>
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 345, in _traverse
e[k] = _traverse(val, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in _traverse
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in <listcomp>
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 345, in _traverse
e[k] = _traverse(val, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in _traverse
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 339, in <listcomp>
return [_traverse(x, visitPre, visitPost) for x in e]
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 345, in _traverse
e[k] = _traverse(val, visitPre, visitPost)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 347, in _traverse
_e = visitPost(e)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\algebra.py", line 142, in translatePName
return prologue.absolutize(p)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\sparql.py", line 374, in absolutize
return self.resolvePName(iri.prefix, iri.localname)
File "C:\Users\Frank\AppData\Local\Programs\Python\Python36-32\lib\site-packag
es\rdflib\plugins\sparql\sparql.py", line 357, in resolvePName
raise Exception('Unknown namespace prefix : %s' % prefix)
Exception: Unknown namespace prefix : dct
提前致谢:)
修改
我认为
出了问题result = g.parse("http://dbpedia.org" + uri2)
此示例中尝试解析的URI是&#34; http://dbpedia.org/resource/Sheldon_J._Plankton&#34;
如果我直接将该URI放在g.parse中,也会出错。这可能是因为URI是#34;错误&#34;,因为它重定向到
&#34; http://dbpedia.org/resource/Plankton_(character)&#34 ;.
我在dbo:wikiPageRedirects的查询中解决了这个问题,但当然这是在解析之后。所以问题就在于我认为,但是如果我不能首先解析它来获取该页面,我怎么能使用dbo获取正确的页面:wikiPageRedirects?
答案 0 :(得分:3)
错误消息抱怨无法识别前缀dct
,RDFLib内置了dcterms
,或者您可以绑定自己的前缀:
from rdflib.namespace import DCTERMS, Namespace
g.bind("dct", DCTerms)
g.bind("dbo", Namespace("http://dbpedia.org/ontology/"))
g.bind("dbr", Namespace("http://dbpedia.org/resource/"))
假设uri2是一个dbpedia资源并且只包含URI的最后部分(即“Sheldon_J._Plankton”),那么获取重定向页面的SPARQL查询将变为:
q = "SELECT ?subject WHERE {{ dbr:{} dbo:wikiPageRedirects ?subject. }}".format
result = g.query(q(uri2))
for row in result:
print(row.subject)
要获取重定向的主题,如果它在您的数据中,则此查询应该有效。但您可能需要在上一个查询中返回的URI上运行g.parse,以将其添加到您的数据中:
q = "SELECT ?subject WHERE {{ dbr:{} dbo:wikiPageRedirects ?redirect. ?redirect dct:subject ?subject. }}".format
result = q.query(q(uri2))