Минимальный воспроизводимый код (написанный в блокноте Jupyter с использованием rdflib 7.1.1):
Код: Выделить всё
from rdflib import URIRef, Graph, Literal
import itertools
def make_example_IRI(ID_list):
return URIRef("http://example.org/" +
'/'.join([str(id).lower()
for id in ID_list])
)
tg = Graph()
tbls = ['A', 'B',]
cols = ['X', 'Y',]
rows = ['i', 'j', 'k']
data = [1,2,3]
label_iri = URIRef('http://www.w3.org/2000/01/rdf-schema#label')
for combo in itertools.product(tbls, rows, cols, data):
row_iri = make_example_IRI([combo[0], 'row', combo[1]])
col_iri = make_example_IRI([combo[0], 'col', combo[2]])
datum = Literal(combo[-1])
tg.add((row_iri, col_iri, datum))
tg.add((col_iri, label_iri, Literal(combo[2])))
bind_list = []
for tab in tbls:
nsR = (f'tab{tab}', make_example_IRI([tab, 'row', '/']))
nsC = (f'tab{tab}_col', make_example_IRI([tab, 'col', '/']))
bind_list.append(nsR)
bind_list.append(nsC)
for short, long in bind_list:
tg.bind(short, long, override=True, replace=True)
Код: Выделить всё
[n for n in tg.namespaces()]
Код: Выделить всё
[ ...
('tabA', rdflib.term.URIRef('http://example.org/a/row//')),
('tabA_col', rdflib.term.URIRef('http://example.org/a/col//')),
('tabB', rdflib.term.URIRef('http://example.org/b/row//')),
('tabB_col', rdflib.term.URIRef('http://example.org/b/col//')),
('tabC', rdflib.term.URIRef('http://example.org/c/row//')),
('tabC_col', rdflib.term.URIRef('http://example.org/c/col//'))]
Код: Выделить всё
tg.serialize(format="turtle", destination="tg.ttl")
Код: Выделить всё
@prefix ns1: .
@prefix ns2: .
@prefix ns3: .
@prefix rdfs: .
@prefix xsd: .
Остальная часть .ttl выглядит примерно так:
Код: Выделить всё
ns2:x rdfs:label "X" .
ns2:y rdfs:label "Y" .
ns2:z rdfs:label "Z" .
ns2:x 1,
2,
3 ;
ns2:y 1,
2,
3 ;
ns2:z 1,
2,
3 .
Код: Выделить всё
tabA:1 tabA_col:x 1,
2,
3 ;
...
Подробнее здесь: https://stackoverflow.com/questions/791 ... urtle-file