summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshley Sommer <ashleysommer@gmail.com>2020-09-17 16:22:17 +1000
committerGitHub <noreply@github.com>2020-09-17 16:22:17 +1000
commitb8ab037f23a204b973dfc31a96155aa9f219138b (patch)
treed4ea0a95102b47c4398e5e37eb776b4e7c03eafb
parent63f46e476e6dd0ae3eef5b71a35e3c8b97a01c6b (diff)
parentc589e762293f3c597a8b671df74c7ef703401efa (diff)
downloadrdflib-b8ab037f23a204b973dfc31a96155aa9f219138b.tar.gz
Merge pull request #1162 from FlorianLudwig/serialize-doc
Small improvement to serialize docs
-rw-r--r--docs/intro_to_parsing.rst12
-rw-r--r--rdflib/graph.py7
2 files changed, 16 insertions, 3 deletions
diff --git a/docs/intro_to_parsing.rst b/docs/intro_to_parsing.rst
index b07b3609..5b50e5ba 100644
--- a/docs/intro_to_parsing.rst
+++ b/docs/intro_to_parsing.rst
@@ -67,3 +67,15 @@ files you'll find on the net.
RDFLib will also happily read RDF from any file-like object,
i.e. anything with a ``.read()`` method.
+
+
+Saving RDF
+----------
+
+To store a graph in a file use the :func:`rdflib.Graph.serialize` function:
+
+.. code-block:: python
+
+ g.parse("http://bigasterisk.com/foaf.rdf")
+ with open("foaf.n3", "wb") as f:
+ g.serialize(f, format="n3") \ No newline at end of file
diff --git a/rdflib/graph.py b/rdflib/graph.py
index b641656d..f6024c46 100644
--- a/rdflib/graph.py
+++ b/rdflib/graph.py
@@ -1,3 +1,4 @@
+from typing import Optional
import logging
from warnings import warn
import random
@@ -957,11 +958,11 @@ class Graph(Node):
def serialize(
self, destination=None, format="xml", base=None, encoding=None, **args
- ):
+ ) -> Optional[bytes]:
"""Serialize the Graph to destination
- If destination is None serialize method returns the serialization as a
- string. Format defaults to xml (AKA rdf/xml).
+ If destination is None serialize method returns the serialization as
+ bytes. Format defaults to xml (AKA rdf/xml).
Format support can be extended with plugins,
but "xml", "n3", "turtle", "nt", "pretty-xml", "trix", "trig" and "nquads" are built in.