python – 在rdflib中使用上下文
发布时间:2020-09-25 20:43:37 所属栏目:Python 来源:互联网
导读:我很难找到一个明确,明智的rdflib使用上下文的例子. ConjunctiveGraph不接受上下文,并且不推荐使用Graph.我应该如何在同一个全局ConjunctiveGraph中的不同上下文中创建和操作? 是.这是代码 import rdflibfrom rdflib.Graph import Graphconj=rdflib.Conjunct
我很难找到一个明确,明智的rdflib使用上下文的例子.
解决方法是.这是代码import rdflib from rdflib.Graph import Graph conj=rdflib.ConjunctiveGraph() NS=rdflib.Namespace("http://example.com/#") NS_CTX=rdflib.Namespace("http://example.com/context/#") alice=NS.alice bob=NS.bob charlie=NS.charlie pizza=NS.pizza meat=NS.meat chocolate=NS.chocolate loves=NS.loves hates=NS.hates likes=NS.likes dislikes=NS.dislikes love_ctx=Graph(conj.store,NS_CTX.love) food_ctx=Graph(conj.store,NS_CTX.food) love_ctx.add( (alice,loves,bob) ) love_ctx.add( (alice,charlie) ) love_ctx.add( (bob,hates,charlie) ) love_ctx.add( (charlie,bob) ) food_ctx.add( (alice,likes,chocolate) ) food_ctx.add( (alice,meat) ) food_ctx.add( (alice,dislikes,pizza) ) print "Full context" for t in conj: print t print "" print "Contexts" for c in conj.contexts(): print c print "love context" for t in love_ctx: print t print "food context" for t in food_ctx: print t 这是输出 Full context (rdflib.URIRef('http://example.com/#bob'),rdflib.URIRef('http://example.com/#hates'),rdflib.URIRef('http://example.com/#charlie')) (rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#likes'),rdflib.URIRef('http://example.com/#chocolate')) (rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#meat')) (rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#dislikes'),rdflib.URIRef('http://example.com/#pizza')) (rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#loves'),rdflib.URIRef('http://example.com/#bob')) (rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#charlie')) (rdflib.URIRef('http://example.com/#charlie'),rdflib.URIRef('http://example.com/#bob')) Contexts <http://example.com/context/#food> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory']. <http://example.com/context/#love> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory']. love context (rdflib.URIRef('http://example.com/#bob'),rdflib.URIRef('http://example.com/#bob')) food context (rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#pizza')) (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 什么是好的编程问题,以在Python中运用“if … else”?
- 想在Jupyter Notebook(Anaconda)中保存并运行Python脚本
- python – tkinter中的标签宽度
- linux 2.6升级Python2.7 ./configure 报错问题
- python – 告诉PyCharm代码生成的类字段
- gettext – Flask-Babel如何在Jinja模板文件中使用翻译
- django sudo runserver启动错误
- dict.viewkeys()返回的数据类型是什么? [python 2.7]
- python – 如何在DataFrame中增加groupby中的行数
- python – 将所有STDIN“啜饮”成字符串的最有效方法
推荐文章
站长推荐
- python – Seaborn / Matplotlib:如何在factorp
- python – boto dynamodb2:我可以只使用范围键查
- Python 2与Python 3 – 过滤器行为的差异
- DAY4(PYTHON)列表的嵌套,range,for
- python proxy-auth中的phantomjs selenium无法正
- python – 使用numpy将alpha通道添加到RGB数组
- python – 扭曲的MySQL adbapi返回字典
- import next()python 2.5
- python – SQLAlchemy吃RAM
- 【Python】一次性查看所有集群节点信息:节点内核
热点阅读