简体   繁体   English

在 Python 中循环查询 SPARQL

[英]looping query SPARQL in Python

I have a task for looping a query SPARQL for each term.我有一个任务,用于为每个术语循环查询 SPARQL。 But i have no idea to process in a code, especially Python.但我不知道在代码中进行处理,尤其是 Python。 I use the RDFlib for Python and my codes show below:我将 RDFlib 用于 Python,我的代码如下所示:

for joined in removed:
print (joined)

output:输出:

sistem
pakar
diagnosis
penyakit
tht
balita
metode
certainty
factor

those terms have through text pre-processing.这些术语已经通过文本预处理。 And after that i want to query those terms but in looping way.之后我想以循环方式查询这些术语。 the query format:查询格式:

qres = g.query(
"""SELECT ?z 
    WHERE {?x rdfs:label ?z .               
          FILTER CONTAINS (LCASE(str(?z)), LCASE ('THE_TERM')) .    
    }
    LIMIT 100""")

i am new to Python, so is there any way to process query for each term consecutively?我是 Python 新手,有什么方法可以连续处理每个术语的查询? or should i use switch and case function?还是应该使用switchcase功能? please advise me, thank you.请给我建议,谢谢。

There is no switch in Python! Python 中没有开关!

See the documentation for rdflib regarding looping through query results here: https://rdflib.readthedocs.io/en/stable/intro_to_sparql.html有关循环查询结果的 rdflib 文档,请参见此处: https ://rdflib.readthedocs.io/en/stable/intro_to_sparql.html

In short:简而言之:

g = Graph()
# do something to load the graph with data

q = g.query("""SOME SPARQL QUERY""")
for r in g.query():
    print(r)
    # or
    print(r['x'])  # if you returned a variable ?x in your SPARQL query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM