-
Notifications
You must be signed in to change notification settings - Fork 0
/
owl2csv.py
48 lines (39 loc) · 1.39 KB
/
owl2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
import click
import json
import csv
from rdflib import Graph, Namespace, URIRef, Literal
def observe_dataset(datasets, query, prefixes, lang):
g = Graph()
if prefixes:
prefixes = json.load(prefixes)
for name, url in prefixes.items():
g.bind(name, Namespace(url.strip('<>')))
for dataset in datasets:
g.parse(dataset)
kwargs = {}
if lang:
kwargs = {'initBindings':{'wantedLang': Literal('fr')}}
return g, g.query(query, **kwargs)
def create_csv(graph, query_result, f):
def normalize_field(field):
if not field:
return None
elif isinstance(field, URIRef):
return field.n3(graph.namespace_manager)
return field
writer = csv.writer(f)
writer.writerow(query_result.vars)
for row in query_result:
writer.writerow(map(normalize_field, row))
@click.command()
@click.argument('datasets', type=click.File('r'), nargs=-1)
@click.argument('query', type=click.File('r'))
@click.option('--prefixes', '-p', default=None, type=click.File('r'))
@click.option('--output', '-o', default=sys.stdout, type=click.File('w'))
@click.option('--lang', '-l', default=None)
def command(datasets, query, prefixes, output, lang):
graph, query_result = observe_dataset(datasets, query, prefixes, lang)
create_csv(graph, query_result, output)
if __name__ == '__main__':
command()