简体   繁体   English

在 Python 中使用 SPARQL 和 RDFLIB 将数据从 Dataframe 插入本体

[英]Inserting Data from Dataframe into Ontology using SPARQL and RDFLIB in Python

I created an ontology using protegee and now want to insert data using RDFLIB in python. Because I have to write the update sparql statements in string and my data comes in various types including, float64, integer, strings, and Datetime, I had to do some parsing and so not all are working.我使用 protegee 创建了一个本体,现在想在 python 中使用 RDFLIB 插入数据。因为我必须用字符串编写更新 sparql 语句,而且我的数据有多种类型,包括 float64、integer、字符串和日期时间,所以我必须这样做一些解析,所以并不是所有的都在工作。 Below is a snippet of my code:下面是我的代码片段:

df=df.tail(2000)
for ind in df.index:
pData=df['Product'][ind]
lData=df['Lifecycle left in minutes'][ind]
XLPerc=df['Percent of Lifecycle left'][ind]


q = """
INSERT DATA
{ 
myontology:XP myontology:LifecycleData XL.
myontology:XP myontology:UseCycleData XU.
#myontology:XP myontology:LifecyclePer XLPerc.
myontology:XP myontology:Temperature XTemperature.
#myontology:XP myontology:LifecyclePer XLPerc

}

""".replace('XU', str(uData)).replace('XL', str(lData)).replace('XP', str(pData))
g.update(
q, 
initNs={
    "myontology":Namespace("https://js......../myontology.owl#")
  }
)

So I am looping over my Dataframe (df) and inserting it into the ontology.因此,我正在遍历我的 Dataframe (df) 并将其插入到本体中。 Some are working and some are not working despite using the same method.尽管使用相同的方法,但有些正在工作,有些却没有工作。 I am getting ParseException error as follows:我收到如下 ParseException 错误:

ParseException: Expected end of text, found 'I' (at char 5), (line:2, col:5) There is a long error code but this is the last line. ParseException: Expected end of text, found 'I' (at char 5), (line:2, col:5)错误代码很长,但这是最后一行。 I can provide more information if needed.如果需要,我可以提供更多信息。 I do not know what the issue is, can somebody help me?我不知道问题是什么,有人可以帮助我吗? Thank you.谢谢你。

I have been able to rectify the problem myself.我已经能够自己解决问题。 The replace() functions were not evaluating correctly due to too similar variables.由于变量太相似, replace()函数无法正确评估。 For instance, myontology:XP myontology:LifecyclePer XLPerc and myontology:XP myontology:LifecycleData XL.例如, myontology:XP myontology:LifecyclePer XLPercmyontology:XP myontology:LifecycleData XL. both had XL as in XLPerc and XL itself.两者都有XLPercXL本身的XL So, while evaluating, the XL in XLPerc was replaced with another value such as 68.23433Perc and not the expected value 68.23433 , and many other similar errors like this.因此,在评估时, XLPerc中的XL被替换为另一个值,例如68.23433Perc而不是预期值68.23433 ,以及许多其他类似的错误。

I solved this by defining my variables as unique as possible and now it is evaluating just fine.我通过尽可能独特地定义我的变量来解决这个问题,现在它正在评估得很好。 Thank you everyone for your help.感谢大家的帮助。

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

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