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
# Signaturenumpy.fromregex(file, regexp, dtype, encoding=None)
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 an os.PathLike
instance.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.The fromregex()
method returns the following value:
ndarray
: This a structured array that contains the part of the file that was matched by regexp
.TypeError
: The fromregex()
method returns a dtype
error when an invalid data type is passed for the structured arrays.
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'])
open()
function to load data.txt
in read-write mode. The fd.write()
function outputs the argument string in the opened file. Finally, the fd.close()
function closes the opened file in the program.np.fromregex()
function to scan the data.txt
file for the specified regexp
criterion. This function has data.txt
, regexp
, and dtype
as arguments and returns a NumPy ndarray
.output['num']
function only prints the numbers from the NumPy ndarray
that was returned.