Search code examples
rdfexport-to-csv

RDF file to excel-readable format


I downloaded an rdf file of the format .ttl - I am new to RDF and I am trying to see if I can get the data in a simple txt/csv format of some sort. Does anyone know how to do this?


Solution

  • RDF has a very simple data model: it's just subject predicate object. You can see this by converting your file to n-triples:

     $ rdfcopy myfile.ttl # apache jena
    
     $ rapper -i turtle myfile.ttl  # rapper (part of librdf)
    

    But this is limited. Suppose you start with the nice looking turtle file:

     @prefix ex: <http://example.com/>
    
     <Brian> ex:age 34 ;
             ex:name "Brian Smith" ;
             ex:homepage <http://my.name.org/Brian> .
    
     <Delia> ex:age 45 ;
             ex:name "Delia Jones" ;
             ex:email <mailto:[email protected]> .
    

    The result is:

    <file:///tmp/Delia> <http://example.com/email> <mailto:[email protected]> .
    <file:///tmp/Delia> <http://example.com/name> "Delia Jones" .
    <file:///tmp/Delia> <http://example.com/age> "45"^^<http://www.w3.org/2001/XMLSchema#integer> .
    <file:///tmp/Brian> <http://example.com/homepage> <http://my.name.org/Brian> .
    <file:///tmp/Brian> <http://example.com/name> "Brian Smith" .
    <file:///tmp/Brian> <http://example.com/age> "34"^^<http://www.w3.org/2001/XMLSchema#integer> .
    

    In other words everything is reduced to three columns.

    You might prefer running a simple sparql query instead. It will give you tabular results of a more useful kind:

    prefix ex: <http://example.com/>
    
    select ?person ?age ?name
    where {
        ?person ex:age ?age ;
                ex:name ?name .
    }
    

    Running that using apache jena's arq:

    $ arq --data myfile.ttl --query query.rq 
    ---------------------------------
    | person  | age | name          |
    =================================
    | <Delia> | 45  | "Delia Jones" |
    | <Brian> | 34  | "Brian Smith" |
    ---------------------------------
    

    which is probably more useful. (You can specify CSV output too by adding --results csv).

    (The librdf equivalent is roqet query.rq --data myfile.ttl -r csv)