我正在尝试使用Python 3.4中的RDFLib生成RDF数据.
一个最小的例子:
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'))
Run Code Online (Sandbox Code Playgroud)
此代码导致以下错误:
file.write(g.serialize(format='turtle'))
TypeError : must be str, not bytes
Run Code Online (Sandbox Code Playgroud)
如果我用以下内容替换最后一行:
file.write(str(g.serialize(format='turtle')))
Run Code Online (Sandbox Code Playgroud)
我没有得到错误,但结果是二进制流的字符串表示(单行文本开头b'):
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> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Jena OntModel来获得本体的直接关系.问题来自listClasses()方法.
我搜索了一段时间在网上的提示,但没有找到我的问题的相关答案.
所以这里有一个完整的例子,其中包含最少的数据和代码,显示出错了什么.
我有一个例如这个基本本体(N-Triple格式化):
<http://weblab.ow2.org/wookie#Anti-social_behaviour> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#Robbery> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#Vehicle_crime> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#Bicycle_theft> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#CriminalEvent> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#Event>.
<http://weblab.ow2.org/wookie#Event> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#WookieThing>.
<http://weblab.ow2.org/wookie#Event> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>.
Run Code Online (Sandbox Code Playgroud)
我基本上希望能够获得所有类,并为每个类获取subClasses.
我使用以下JAVA代码:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.util.FileManager;
public class Main {
public static void main(final String [] argv) throws FileNotFoundException, IOException {
final OntModel model = ModelFactory.createOntologyModel(); …Run Code Online (Sandbox Code Playgroud)