numpy.fromregex(file, regexp, dtype, encoding=None)
[source]
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.
Parameters: |
|
---|---|
Returns: |
|
Raises: |
|
See also
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 doc.structured_arrays
.
>>> f = open('test.dat', 'w') >>> _ = f.write("1312 foo\n1534 bar\n444 qux") >>> f.close()
>>> regexp = r"(\d+)\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex('test.dat', regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312, b'foo'), (1534, b'bar'), ( 444, b'qux')], dtype=[('num', '<i8'), ('key', 'S3')]) >>> output['num'] array([1312, 1534, 444])
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.fromregex.html