-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertir-csv-ttl.py
executable file
·55 lines (42 loc) · 1.25 KB
/
convertir-csv-ttl.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
49
50
51
52
53
54
55
#!/usr/bin/env python3
'''
1. convertir les termes de vocabulaire : CSV -> TTL
2. concaténer
'''
import csv, re, shutil
# config
vocabFile = 'vocab/vocab.csv'
vocabFileTTL = 'vocab/vocab.ttl'
TBox = 'qdmtl.ttl'
# Initialisations
extractedData = []
'''
1. convertir
'''
# Chargement des données
with open(vocabFile, 'r+', newline = '') as csvFile:
for row in csv.DictReader(csvFile, delimiter = ','):
extractedData.append(dict(row))
# Écriture du fichier Turtle
with open(vocabFileTTL, 'w') as ABox:
for subject in extractedData:
for i, (property, value) in enumerate(subject.items()):
if value == '':
continue
if 1 < i < len(subject):
assertions += ' ;\n'
# voir structure switch Python 3.10
if property == 'URI':
assertions = value + ' rdf:type owl:NamedIndividual ,\n'
elif property == 'rdf:type':
assertions += '\t\t' + subject['rdf:type']
else:
assertions += '\t' + property + ' ' + value
assertions += ' .\n\n'
ABox.write(assertions)
'''
2. concaténer
'''
with open(TBox,'a') as file:
with open(vocabFileTTL,'r') as input:
shutil.copyfileobj(input, file)