bundles / numpy 2.4.3 / numpy / fromregex
function
numpy:fromregex
source: /numpy/lib/_npyio_impl.py :1633
Signature
def fromregex ( file , regexp , dtype , encoding = None ) Summary
Construct an array from a text file, using regular expression parsing.
Extended Summary
The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array.
Parameters
file: file, str, or pathlib.PathFilename or file object to read.
regexp: str or regexpRegular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype.
dtype: dtype or list of dtypesDtype for the structured array; must be a structured datatype.
encoding: str, optionalEncoding used to decode the inputfile. Does not apply to input streams.
Returns
output: ndarrayThe output array, containing the part of the content of
filethat was matched byregexp. output is always a structured array.
Raises
: TypeErrorWhen
dtypeis not a valid dtype for a structured array.
Notes
Dtypes for structured arrays can be specified in several forms, but all forms specify at least the data type and field name. For details see basics.rec.
Examples
import numpy as np from io import StringIO text = StringIO("1312 foo\n1534 bar\n444 qux")✓
regexp = r"(\d+)\s+(...)" # match [digits, whitespace, anything] output = np.fromregex(text, regexp, [('num', np.int64), ('key', 'S3')]) output output['num']✓
See also
- fromstring
- loadtxt
Aliases
-
numpy.fromregex