Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvement to serialize docs #1162

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/intro_to_parsing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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")
7 changes: 4 additions & 3 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
import logging
from warnings import warn
import random
Expand Down Expand Up @@ -957,11 +958,11 @@ def absolutize(self, uri, defrag=1):

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.
Expand Down