MCPcopy Index your code
hub / github.com/numpy/numpy / fromregex

Function fromregex

numpy/lib/_npyio_impl.py:1632–1725  ·  view source on GitHub ↗

r""" Construct an array from a text file, using regular expression parsing. 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.

(file, regexp, dtype, encoding=None)

Source from the content-addressed store, hash-verified

1630
1631@set_module('numpy')
1632def fromregex(file, regexp, dtype, encoding=None):
1633 r"""
1634 Construct an array from a text file, using regular expression parsing.
1635
1636 The returned array is always a structured array, and is constructed from
1637 all matches of the regular expression in the file. Groups in the regular
1638 expression are converted to fields of the structured array.
1639
1640 Parameters
1641 ----------
1642 file : file, str, or pathlib.Path
1643 Filename or file object to read.
1644
1645 .. versionchanged:: 1.22.0
1646 Now accepts `os.PathLike` implementations.
1647
1648 regexp : str or regexp
1649 Regular expression used to parse the file.
1650 Groups in the regular expression correspond to fields in the dtype.
1651 dtype : dtype or list of dtypes
1652 Dtype for the structured array; must be a structured datatype.
1653 encoding : str, optional
1654 Encoding used to decode the inputfile. Does not apply to input streams.
1655
1656 Returns
1657 -------
1658 output : ndarray
1659 The output array, containing the part of the content of `file` that
1660 was matched by `regexp`. `output` is always a structured array.
1661
1662 Raises
1663 ------
1664 TypeError
1665 When `dtype` is not a valid dtype for a structured array.
1666
1667 See Also
1668 --------
1669 fromstring, loadtxt
1670
1671 Notes
1672 -----
1673 Dtypes for structured arrays can be specified in several forms, but all
1674 forms specify at least the data type and field name. For details see
1675 `basics.rec`.
1676
1677 Examples
1678 --------
1679 >>> import numpy as np
1680 >>> from io import StringIO
1681 >>> text = StringIO("1312 foo\n1534 bar\n444 qux")
1682
1683 >>> regexp = r"(\d+)\s+(...)" # match [digits, whitespace, anything]
1684 >>> output = np.fromregex(text, regexp,
1685 ... [('num', np.int64), ('key', 'S3')])
1686 >>> output
1687 array([(1312, b'foo'), (1534, b'bar'), ( 444, b'qux')],
1688 dtype=[('num', '<i8'), ('key', 'S3')])
1689 >>> output['num']

Callers

nothing calls this directly

Calls 7

asbytesFunction · 0.90
openMethod · 0.45
dtypeMethod · 0.45
readMethod · 0.45
compileMethod · 0.45
viewMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…