Validating RDF Files Using Raptor or Sax

Given an RDF file, I want to write a python script to check the file and comment if it is incorrect. Hou, am I doing this with RAptor? or Sax or is there another library? No luck with w3.

+4
source share
1 answer

You have two options with raptor:

Option 1: Use the rapper command line, it is very fast. Below is the function below in python to complete the command. The -c option is to simply count the number of triples. The lang parameter is just an option to specify ntriples of the RDF, rdfxml, turtle, ... format. The function checks the return code and throws an exception in case something goes wrong.

 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]) 

Option 2: Use Python bindings. The following will work:

 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" 

This code has been extracted from example.py , check it out and you will see more examples.

+2
source

Source: https://habr.com/ru/post/1395262/


All Articles