Comments (4)
@mohmehmo
Another name for a RDF "list" is a Collection.
See the docs for RDF Collection
here: https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.Graph.collection
See this example for how to use it:
g = rdflib.Graph()
my_list = g.collection(rdflib.URIRef("http://example.org/my_list"))
my_list.append(rdflib.URIRef("http://example.org/Huey"))
my_list.append(rdflib.URIRef("http://example.org/Dewey"))
my_list.append(rdflib.URIRef("http://example.org/Louie"))
g.add((URIRef("http://example.org/Donald"), URIRef("http://example.org/nephews"), my_list.uri))
n3_graph = g.serialize(format="n3")
from rdflib.
Hi @mohmehmo
I've tested your snippet above. In this case, the object in your triple should be myList
or c.uri
, both work.
from rdflib import Graph, BNode, Literal, URIRef
from rdflib.collection import Collection
myList = BNode()
g = Graph()
c = Collection(g, myList)
for item in [1, 2, 3, 4, 5]:
c.append(Literal(item))
g.add((URIRef("http://example.org/"),URIRef("http://example.org/my_list"), myList))
# Test it:
print(g.serialize(format="turtle"))
or
from rdflib import Graph, BNode, Literal, URIRef
from rdflib.collection import Collection
myList = BNode()
g = Graph()
c = Collection(g, myList)
for item in [1, 2, 3, 4, 5]:
c.append(Literal(item))
g.add((URIRef("http://example.org/"),URIRef("http://example.org/my_list"), c.uri))
# Test it:
print(g.serialize(format="turtle"))
from rdflib.
@ashleysommer
Thanks for your response. Your suggestion resolved a part of my problem. Consider I have the following script to create a list as a Collection.
myList= BNode()
g = Graph('Memory')
c = Collection(g, myList)
for item in [1, 2, 3, 4, 5]:
c.append(Literal(item))
When I want to add c
as an object of (S, P, O) to the main graph, I faced with an error saying "<object> must be an rdflib term". I have tried different approaches e.g. URIRef(c)
, c.uri
, c.n3()
, but none of them works without error.
Do you have any suggestions for resolving this issue?
from rdflib.
@ashleysommer
Thanks a lot for your support. it is fine now.
from rdflib.
Related Issues (20)
- Incorrect working when deleting triplets in a BerkeleyDB store HOT 3
- Graph.parse: Guess format when remote source content type is "text/plain"
- Optional skolemize blank nodes on parse HOT 4
- json-ld serialize doesnt failt or raise warning, when producing invalid syntax; type is literal HOT 7
- FAILED test/test_misc/test_parse_file_guess_format.py::TestFileParserGuessFormat::test_warning HOT 2
- RFE: move away from deprecated `html5lib` HOT 1
- BUG : jsonld parser does not randomise bnodes when using local _prefixed identifiers `"@id": "_:mybnode01"` HOT 1
- Bug in `longturtle` serialization HOT 3
- Rdflib 7.0.0: Inadequate Support for Importing Multiple Prefixes with the Same IRI and Base IRI HOT 2
- Unexpected recursive context inclusion exception HOT 2
- Bug: Unexpected namespace creation during turtle file serialization HOT 4
- Using `orjson` instead of default `json` library HOT 1
- Error when parsing turtle file HOT 4
- Documentation of collection.py can be improved
- TestUtil.test_to_bits_from_bits_round_trip test failure with python3.13b2 HOT 4
- `rdfpipe` and binding XSD prefix
- HextuplesParser could/should use json.loads with strict=False
- regenerate _DOAP.py HOT 3
- JSON-LD serialization apparently loses XSD type information. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rdflib.