w3resource

NumPy Input and Output: fromregex() function

numpy.fromregex() function

The fromregex() function is used to 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.

Syntax:

numpy.fromregex(file, regexp, dtype, encoding=None)

Version: 1.15.0

Parameter:

Name Description Required /
Optional
file File name or file object to read.
str or file
Required
regexp Regular expression used to parse the file.
Groups in the regular expression correspond to fields in the dtype.
str or regexp
Required
dtype Dtype for the structured array.
dtype or list of dtypes
Required
encoding Encoding used to decode the inputfile.
Does not apply to input streams.
str
Optional

Returns: output : ndarray
The output array, containing the part of the content of file that was matched by regexp. output is always a structured array.

Raises:
TypeError : When dtype is 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 doc.structured_arrays.

NumPy.fromregex() method Example-1:

Store data to disk, and load it again:

>>> import numpy as np
>>> x = open('test.dat', 'w')
>>> x.write("1413 foo\n1354  bar\n222   qux")
>>> x.close()
>>> regexp = r"(\d+)\s+(...)"  # match [digits, whitespace, anything]
>>> output = np.fromregex('test.dat', regexp,
...                       [('num', np.int64), ('key', 'S3')])
>>> output

Output:

array([(1413, b'foo'), (1354, b'bar'), ( 222, b'qux')],
      dtype=[('num', '<i8'), ('key', 'S3')])

NumPy.fromregex() method Example-2:

>>> import numpy as np
>>> x = open('test.dat', 'w')
>>> x.write("1413 foo\n1354  bar\n222   qux")
>>> x.close()
>>> regexp = r"(\d+)\s+(...)"  # match [digits, whitespace, anything]
>>> output['num']

Output:

array([1413, 1354,  222], dtype=int64)

Python - NumPy Code Editor:

Previous: genfromtxt() function
Next: fromstring() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/input-and-output/fromregex.php