What is the fromregex() method in NumPy?
Overview
NumPy is a Python library which is used for efficient data processing with additional support for multi-dimensional arrays and matrices.
The fromregex() method is used to make an array from a text file by using
Syntax
# Signaturenumpy.fromregex(file, regexp, dtype, encoding=None)
Parameter values
The fromregex() method takes the following parameter values:
file: This is a path or file. It is a required parameter value. It takes a filename or a file instance to read. It also accepts anos.PathLikeinstance.
regexp: This is the regular expression that is used to parse the file. It is a required parameter value.dtype: This is the DataType for the structured arrays. It is a required parameter value.encoding: This is the encoding scheme that is used to encode the input file. The default value of this parameter is "none." It is an optional parameter value.
Return value
The fromregex() method returns the following value:
ndarray: This a structured array that contains the part of the file that was matched byregexp.
Exception
TypeError: The fromregex() method returns a dtype error when an invalid data type is passed for the structured arrays.
Example
In the code snippet below, we are going to discuss the working of the fromregex() method. This method takes a data file, a regex, and a dtype as parameter values and returns the scanned file as ndarray.
# program to test fromregex() functionimport numpy as np# opening file in reading writing mode w+fd = open('data.txt', 'w+')# writing to filefd.write("Tim Bachlaka 54642 \nRoger Field 76548 \nFaheem Ahmad 76543 \n end")# closing filefd.close()# a regular expression to match digits, whitespace or anythingregexp = r"(\d+)\s+(...)"# ndarray in output variableoutput = np.fromregex('data.txt', regexp, [('num', np.int64), ('key', 'S3')])print(output['num'])
Explanation
- Lines 4–8: We use the
open()function to loaddata.txtin read-write mode. Thefd.write()function outputs the argument string in the opened file. Finally, thefd.close()function closes the opened file in the program.
- Line 10: We create a regular expression to scan digits, spaces, and anything else that may be present.
- Line 12: We invoke the
np.fromregex()function to scan thedata.txtfile for the specifiedregexpcriterion. This function hasdata.txt,regexp, anddtypeas arguments and returns a NumPyndarray. - Line 13: The
output['num']function only prints the numbers from the NumPyndarraythat was returned.