Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从rdflib中选择值_Python_Rdflib - Fatal编程技术网

Python 从rdflib中选择值

Python 从rdflib中选择值,python,rdflib,Python,Rdflib,我是rdflib新手,我使用它在一个简单的turtle文件上运行查询。我正在解析的数据如下 a ns1:站点a ns1:物料流对象; ns1:hasName PolishingStation1。 a ns1:站点a ns1:物料流对象; ns1:hasName PolishingStation2。 a ns1:站点a ns1:物料流对象; ns1:hasName-QuenchingStation1。 a ns1:站点a ns1:物料流对象; ns1:hasName-QuenchingStatio

我是rdflib新手,我使用它在一个简单的turtle文件上运行查询。我正在解析的数据如下

a ns1:站点a ns1:物料流对象; ns1:hasName PolishingStation1。 a ns1:站点a ns1:物料流对象; ns1:hasName PolishingStation2。 a ns1:站点a ns1:物料流对象; ns1:hasName-QuenchingStation1。 a ns1:站点a ns1:物料流对象; ns1:hasName-QuenchingStation2。 a ns1:站点a ns1:物料流对象; ns1:hasName-QuenchingStation3。 a ns1:站点a ns1:物料流对象; ns1:hasName-QuenchingStation4

查询如下

`选择“名称”类型 从…起 哪里 {

}`

问题是,当我得到类型时,每个变量有2个值,我根本无法将它们分开,我正在使用的代码

kg=结膜记录器 输入\本体\路径=./plant\ u data.ttl kg.parseinput\u ontology\u路径,格式=ttl

使用openquerys/get_Material_Flow.rq作为f: sparql\u query\u string=f.read query\u output=kg.querysparql\u query\u字符串 对于查询输出中的行: d=str行['type'] s=d.split[-1] 印刷品 pprint.pprintow

我得到的结果总是一个包含两个值的字符串 排水 物流对象
在一个字符串中,我不能用拆分字符串或其他任何东西将它们分开,我所需要的只是在一个不同于MaterialFlowObject的变量中获取Drain,不管怎样,

修复了示例文件:

plant_data.ttl:

运行_query.py:

这将按预期工作,并为每个匹配生成一个结果:

name=PolishingStation1,type=tag:a#Station
name=PolishingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#Station
name=QuenchingStation4,type=tag:a#Station
name=QuenchingStation4,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#Station
name=QuenchingStation2,type=tag:a#MaterialFlowObject
name=QuenchingStation2,type=tag:a#Station
name=QuenchingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation1,type=tag:a#Station

修复示例文件后:

plant_data.ttl:

运行_query.py:

这将按预期工作,并为每个匹配生成一个结果:

name=PolishingStation1,type=tag:a#Station
name=PolishingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#Station
name=QuenchingStation4,type=tag:a#Station
name=QuenchingStation4,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#Station
name=QuenchingStation2,type=tag:a#MaterialFlowObject
name=QuenchingStation2,type=tag:a#Station
name=QuenchingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation1,type=tag:a#Station
您的SPARQL引用machines.ttl,但您也在脚本中解析plant_data.ttl。您的查询引用了ns1:Drain,但您的数据使用了ns1:Station。您的文件使用但不定义ns1:前缀。请发布一个。您的SPARQL引用machines.ttl,但您也在解析脚本中的plant_data.ttl。您的查询引用了ns1:Drain,但您的数据使用了ns1:Station。您的文件使用但不定义ns1:前缀。请发一封信。
PREFIX ns1: <tag:a#>

SELECT ?name ?type 
WHERE {
  ?MaterialFlowObject a ns1:Station.
  ?MaterialFlowObject a ?type.
  ?MaterialFlowObject ns1:hasName ?name.
}
from rdflib.graph import ConjunctiveGraph

kg = ConjunctiveGraph() 
input_ontology_path = "./plant_data.ttl" 
kg.parse(input_ontology_path, format="ttl")

with open("get_Material_Flow.rq") as f:
    sparql_query_string = f.read() 
    query_output = kg.query(sparql_query_string) 
    for row in query_output: 
        print(f'name={str(row["name"])},type={str(row["type"])}')
name=PolishingStation1,type=tag:a#Station
name=PolishingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#Station
name=QuenchingStation4,type=tag:a#Station
name=QuenchingStation4,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#Station
name=QuenchingStation2,type=tag:a#MaterialFlowObject
name=QuenchingStation2,type=tag:a#Station
name=QuenchingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation1,type=tag:a#Station