load¶
- nestedtext.load(f, top='dict', *, source=None, on_dup=None, keymap=None, normalize_key=None)[source]¶
Loads NestedText from file or stream.
Is the same as
loads()
except the NextedText is accessed by reading a file rather than directly from a string. It does not keep the full contents of the file in memory and so is more memory efficient with large files.- Parameters
f (str, os.PathLike, io.TextIOBase, collections.abc.Iterator) – The file to read the NestedText content from. This can be specified either as a path (e.g. a string or a pathlib.Path), as a text IO object (e.g. an open file, or 0 for stdin), or as an iterator. If a path is given, the file will be opened, read, and closed. If an IO object is given, it will be read and not closed; utf-8 encoding should be used.. If an iterator is given, it should generate full lines in the same manner that iterating on a file descriptor would.
kwargs – See
loads()
for optional arguments.
- Returns
The extracted data. See
loads()
description of the return value.- Raises
NestedTextError – if there is a problem in the NextedText document.
OSError – if there is a problem opening the file.
Examples
Load from a path specified as a string:
>>> import nestedtext as nt >>> print(open("examples/groceries.nt").read()) groceries: - Bread - Peanut butter - Jam >>> nt.load("examples/groceries.nt") {'groceries': ['Bread', 'Peanut butter', 'Jam']}
Load from a pathlib.Path:
>>> from pathlib import Path >>> nt.load(Path("examples/groceries.nt")) {'groceries': ['Bread', 'Peanut butter', 'Jam']}
Load from an open file object:
>>> with open("examples/groceries.nt") as f: ... nt.load(f) ... {'groceries': ['Bread', 'Peanut butter', 'Jam']}