gsplot.read_array#

read_array(path, *, loader='genfromtxt', ndmin=1, options=None)#

Read a text array without changing the process working directory.

options is passed to the selected NumPy loader after the two gsplot controls are validated. In particular, unpack and structured-array options retain NumPy’s documented behavior.

Parameters:
  • path (str | PathLike[str]) – Text file to read. The current working directory is not changed.

  • loader (Literal['genfromtxt', 'loadtxt']) – NumPy loader name: "genfromtxt" or "loadtxt".

  • ndmin (int) – Minimum number of dimensions passed to NumPy.

  • options (Mapping[str, Any] | None) – Finite mapping of options for the selected NumPy loader.

Returns:

Native loader result. Structured dtype with unpack=True may return separate field arrays with different dtypes.

Return type:

numpy.ndarray or list of numpy.ndarray

Raises:

DataError – If the path, loader controls, options, or file contents are invalid.

Examples

>>> from pathlib import Path
>>> from tempfile import TemporaryDirectory
>>> import gsplot as gs
>>> with TemporaryDirectory() as directory:
...     path = Path(directory) / "data.txt"
...     _ = path.write_text("0 1\n2 3\n", encoding="utf-8")
...     array = gs.read_array(path, loader="genfromtxt")
>>> array.shape
(2, 2)