Python中文
首页
教程
问答
标签
搜索
登录
注册
如何使用RDFLib导出RDF文件中的图形
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图在Python3.4中使用RDFLib生成RDF数据。</p> <p>一个简单的例子:</p> <pre class="lang-py prettyprint-override"><code>from rdflib import Namespace, URIRef, Graph from rdflib.namespace import RDF, FOAF data = Namespace("http://www.example.org#") g = Graph() g.add( (URIRef(data.Alice), RDF.type , FOAF.person) ) g.add( (URIRef(data.Bob), RDF.type , FOAF.person) ) g.add( (URIRef(data.Alice), FOAF.knows, URIRef(data.Bob)) ) #write attempt file = open("output.txt", mode="w") file.write(g.serialize(format='turtle')) </code></pre> <p>此代码导致以下错误:</p> <pre><code>file.write(g.serialize(format='turtle')) TypeError : must be str, not bytes </code></pre> <p>如果我将最后一行替换为: </p> <pre><code>file.write(str(g.serialize(format='turtle'))) </code></pre> <p>我没有得到错误,但结果是二进制流的字符串表示形式(以<code>b'</code>开头的单行文本):</p> <pre><code>b'@prefix ns1: <http://xmlns.com/foaf/0.1/> .\n@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix xml: <http://www.w3.org/XML/1998/namespace> .\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n<http://www.example.org#Alice> a ns1:person ;\n ns1:knows <http://www.example.org#Bob> .\n\n<http://www.example.org#Bob> a ns1:person .\n\n' </code></pre> <p><strong>问题</strong> 如何正确地将图形导出到文件中?</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>在函数中写入文件名对我有效:</p> <pre><code>g.serialize('output_file.ttl',format='ttl') </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Kerasterflow预训练模型中的纯训练偏差
4 回答
KerasTF Conv2D模型运行时无响应型号.fi
10 回答
Kerastuner Randomsearch:TypeError:(“关键字参数未理解:”,“激活”)
4 回答
Kerastuner ValueError:形状(320,)和(1,)不兼容
3 回答
Kerastuner:“ValueError:不是法律参数”问题,当我使用LSTM网络时,但密集层工作正常
5 回答
KerasTuner:是否可以在目标/度量函数中使用测试/验证集?
4 回答
KerasTuner自定义目标函数
6 回答
kerastuner调整层数会创建与报告的层数不同的层数
6 回答
KerasTuner运行时错误:构建模型的失败尝试太多
4 回答
kerasv1.2.2与kerasv2+的奇怪行为(精确度上的巨大差异)
3 回答
kerasvis中visualize_-cam/visualize_显著性的热图输出形状
1 回答
Kerasvis和tfkerasvis的激活最大化不适用于MobileNetV2模型
4 回答
Kerasvis对于显著性图表,我们应该使用softmax还是线性激活
5 回答
Kerasvis给出以下错误:AttributeError:多个入站节点
6 回答
kerasyolov3模型中预期输入和目标的格式和形状
5 回答
Keras一个GPU可以同时训练两个不相关的模型吗?
10 回答
Keras一类CNN两个输入,每一步一个
5 回答
keras三维张量上的Softmax层
7 回答
Keras三维目标预测
2 回答
keras上的flatten与python中的Image的区别
3 回答