在 python 中将线条读入图形时,RDFlib 接受哪些不同的格式? [英] What different formats does RDFlib accept when reading lines into a graph in python?
问题描述
RDFlib 是否可以将 xml 代码行放入 Python 图形中?我知道三元组通常与 RDFlib 一起使用,但如果我不必将它们从 XML 转换为 Turtle,它将节省大量工作.
Can RDFlib take in lines of xml code to be put into a graph in python? I Know triples are generally what is used with RDFlib, but if I didn't have to convert them from XML to Turtle it would save a lot of work.
推荐答案
假设当您说xml 代码"时,您实际上指的是 RDF/XML,并且您拥有整个 RDF/XML 文档,而不仅仅是它的一个片段,那么是的,RDFlib 接受它.文档页面 加载和保存 RDF 以 N-Triples 中的示例开始,但是提到如何指定其他格式:
Assuming that when you say "xml code" you actually mean RDF/XML, and that you have the entire RDF/XML document, not just a snippet of it, then yes, RDFlib accepts it. The documentation page Loading and saving RDF starts with an example in N-Triples, but mentions how other formats are specified:
RDF 数据具有多种语法(xml
、n3
、ntriples
、trix
等),您可能想读.最简单的格式是 ntriples
,一种基于行的格式.用这两行在当前目录下创建文件demo.nt:
Reading an NT file
RDF data has various syntaxes (
xml
,n3
,ntriples
,trix
, etc) that you might want to read. The simplest format isntriples
, a line-based format. Create the file demo.nt in the current directory with these two lines:
<http://bigasterisk.com/foaf.rdf#drewp> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
<http://bigasterisk.com/foaf.rdf#drewp> <http://example.com/says> "Hello world" .
你需要告诉RDFLib解析什么格式,使用format
关键字参数给parse()
,你可以传递一个mime-type 或名称(可用解析器列表 可用).如果您不确定您的文件是什么格式,您可以使用 rdflib.util.guess_format()
将根据文件扩展名猜测.
You need to tell RDFLib what format to parse, use the format
keyword-parameter to parse()
, you can pass either a mime-type or the name (a list of available parsers is available). If you are not sure what format your file will be, you can use rdflib.util.guess_format()
which will guess based on the file extension.
在您的情况下,您只需要使用 xml
(实际上意味着 RDF/XML)格式:
In your case, you just need to use the xml
(which actually means RDF/XML) format:
>>> g.parse("demo.rdf", format="xml")
这篇关于在 python 中将线条读入图形时,RDFlib 接受哪些不同的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!