簡體   English   中英

RDFLIB:SPARQL 計數查詢給出了不適當的輸出

[英]RDFLIB : SPARQL count query gives an inappropriate output

我是 python 和 SPARQL 的新手。 我正在嘗試從 .ttl 文件列表中獲取主題為“社會科學”的元數據的計數。 我正在使用下面的代碼。 但是,我得到的輸出似乎是一個錯誤。 有人可以闡明一下嗎?

files = Path(r'D:\Masterproject\Metadata_PP\Test_Input').glob('*.ttl')
graph = Graph()
for file in files:
    graph.parse(file, format='ttl')

knows_query ="""
SELECT ?mdata (count(distinct ?mdata) as ?count)
WHERE {
?mdata dcterms:creator ?author .
?mdata dcterms:title ?title .
?mdata dcterms:subject ?area .
?mdata dcterms:subject ?o FILTER ( str(?o) = "Social sciences" ) . 
}
GROUP BY ?mdata
"""

qres = graph.query(knows_query)
#to append the SPARQL results in an output .txt file
for row in qres:
    mdata = (f"{row.mdata}|{row.count}")
    print(mdata)

我得到的輸出是:

http://doi.org/10.17026/dans-zgj-fdpf|<built-in method count of ResultRow object at 0x000002485D5198B0>

在第二列中,我希望輸出為“1”

而不是使用{row.count} ,使用{row['count']} 這將避免使用count方法的沖突標識符。 (或者,您可以將?count方法重命名為不會與count方法沖突的其他名稱。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM