Examples from the lectures: Difference between revisions
From info216
(Created page with "This page contains code examples from the lectures. ==S02== <nowiki>from inspect import formatargspec from rdflib import Graph, Literal, RDF, FOAF, Namespace g = Graph() E...") |
(→S02) |
||
Line 3: | Line 3: | ||
==S02== | ==S02== | ||
< | <syntaxhighlight> | ||
from inspect import formatargspec | |||
from rdflib import Graph, Literal, RDF, FOAF, Namespace | from rdflib import Graph, Literal, RDF, FOAF, Namespace | ||
Line 31: | Line 32: | ||
g2.parse('https://www.wikidata.org/entity/Q935079.ttl') | g2.parse('https://www.wikidata.org/entity/Q935079.ttl') | ||
print(g2.serialize()) | print(g2.serialize()) | ||
</ | </syntaxhighlight> | ||
==S03== | ==S03== | ||
==S04== | ==S04== |
Revision as of 13:00, 21 February 2022
This page contains code examples from the lectures.
S02
from inspect import formatargspec
from rdflib import Graph, Literal, RDF, FOAF, Namespace
g = Graph()
EX = Namespace('http://ex.org/')
g.add((EX.Bob, RDF.type, FOAF.Person))
g.add((EX.Alice, RDF.type, FOAF.Person))
g.add((EX.Carol, RDF.type, FOAF.Person))
g.add((EX.Bob, FOAF.knows, EX.Alice))
g.add((EX.Bob, FOAF.knows, EX.Carol))
g.add((EX.Bob, FOAF.name, Literal('Bob')))
g.add((EX.Alice, FOAF.name, Literal('Alice')))
g.add((EX.Carol, FOAF.name, Literal('Carol')))
g.namespace_manager.bind('ex', EX)
print(g.serialize(format='json-ld'))
for p, o in g[ EX.Bob : : ]:
print(p, o)
g2 = Graph()
g2.parse('https://www.wikidata.org/entity/Q935079.ttl')
print(g2.serialize())