File: example-insert-using-rdflib.py

package info (click to toggle)
sparql-wrapper-python 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,228 kB
  • sloc: python: 14,201; makefile: 30
file content (28 lines) | stat: -rw-r--r-- 996 bytes parent folder | download
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
from rdflib import Graph, URIRef, Literal
from rdflib.namespace import RDFS

g = Graph()
asturias = URIRef("http://dbpedia.org/resource/Asturias")
g.add( (asturias, RDFS.label, Literal('Asturies', lang="ast") ) )
g.add( (asturias, RDFS.label, Literal('Asturias', lang="es") ) )
g.add( (asturias, RDFS.label, Literal('Asturien', lang="de") ) )

###############################################################################
triples = ""
for subject,predicate,obj in g:
    triples = triples + subject.n3() + " " + predicate.n3() + " " + obj.n3() + " . \n"

query = """WITH <http://example.graph>
INSERT {"""+ triples + """}"""
###############################################################################

from SPARQLWrapper import SPARQLWrapper, POST, DIGEST
  
sparql = SPARQLWrapper("https://example.org/sparql-auth")
sparql.setHTTPAuth(DIGEST)
sparql.setCredentials("login", "password")
sparql.setMethod(POST)
sparql.setQuery(query)

results = sparql.query()
print(results.response.read())