forked from SemanticLab/simple-csv-to-rdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_with_quads.py
34 lines (21 loc) · 1.05 KB
/
convert_with_quads.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
import csv
from rdflib import Graph, Literal, Namespace, URIRef
from rdflib.namespace import DCTERMS, RDF, RDFS, SKOS, XSD
input_file = csv.DictReader(open("test_sheet.csv"))
# make a graph
for row in input_file:
# convert it from an OrderedDict to a regular dict
row = dict(row)
# print(row)
#{'Subject Label': 'Pearl Wilmer Booker', 'Subject URI': 'None', 'Predicate Label': 'Daughter Of', 'Predicate URI': '', 'Predicate Symmetry': 'Asymmetric', 'Object Label': 'Mary Booker', 'Object URI': 'None'}
# make a literal and add it
# output_graph.add( (URIRef(row['Subject URI']), RDFS.label, Literal(row['Subject Label'], lang='en')) )
output_graph = Graph()
# make a triple with the object as uri
output_graph.add( (URIRef(row['Subject URI']), URIRef(row['Predicate URI']), URIRef(row['Object URI'])) )
triple = output_graph.serialize(format='nt')
triple = str(triple, 'utf-8').strip()
triple = triple.replace('.','')
triple = f"{triple} <{row['Context']}> ."
print(triple)
# output_graph.serialize(destination='my_graph.nt', format='nt')