NumPy Reading Text Files
loadtxt can read a regular numerical table from text. Declare the delimiter and dtype, then check dimensions and finite values before calculating a report.
How it works
A simple comma-separated numeric fixture is different from a general CSV file containing quoted fields, missing text and mixed types. Choose a more suitable parser for those cases rather than assuming loadtxt implements every spreadsheet convention. Keep headers, units and shape expectations in the input contract. The in-memory StringIO example is supported online because it does not access a personal filesystem or network.
Run the code
Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.
For local work, use a separate virtual environment and run python text-input.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.
Example
import numpy as np
from io import StringIO
table = np.loadtxt(StringIO("12,18,24\n6,9,15\n"), delimiter=",", dtype=np.float64)
if table.shape != (2, 3) or not np.isfinite(table).all():
raise ValueError("Expected two sites and three finite days")
print(table.sum(axis=1).tolist())
Expected result
[54.0, 30.0]
The parser supplies a numeric array and the explicit checks make the expected table shape visible. A malformed row or non-numeric token should reject the fixture instead of being treated as an ordinary zero.
Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.