使用Raptor或Sax验证RDF文件

mad*_*ode 5 python validation rdf sax redland

给定一个RDF文件,我想编写一个python脚本来验证文件并注释格式是否错误.我是用RAptor做的吗?还是Sax还是有其他图书馆吗?w3没有运气.

Man*_*res 3

猛禽有两种选择:

选项 1:使用rapper命令行,这非常快。下面的函数是 python 中的一个示例,用于包装该命令。选项-c是只计算三元组的数量。该参数lang只是指定 RDF 格式 ntriples、rdfxml、turtle 等的选项。该函数检查返回代码并在出现任何问题时抛出异常。

def rapper_count(f,lang):
    p=subprocess.Popen(["rapper","-i",lang,"-c",f],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    output, err = p.communicate()
    ret = p.poll()
    if ret <> 0:
        raise Exception, "Error parsing with rapper\n%s"%err
    return int(err.split()[-2])
Run Code Online (Sandbox Code Playgroud)

选项 2:使用 redland Python 语言绑定。像下面这样的东西会起作用:

import RDF

test_file = "/some/file"

uri=RDF.Uri(string="file:"+test_file)

parser=RDF.Parser(name="turtle")
if parser is None:
  raise Exception("Failed to create RDF.Parser raptor")

count=0
for s in parser.parse_as_stream(uri,uri):
  count=count+1

print "Parsing added",count,"statements"
Run Code Online (Sandbox Code Playgroud)

这段代码是从example.py中提取的,检查一下,您会看到更多示例。