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 regular expressionRegex parsing.

Syntax


# Signature
numpy.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 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.

Return 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.

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() function
import numpy as np
# opening file in reading writing mode w+
fd = open('data.txt', 'w+')
# writing to file
fd.write("Tim Bachlaka 54642 \nRoger Field 76548 \nFaheem Ahmad 76543 \n end")
# closing file
fd.close()
# a regular expression to match digits, whitespace or anything
regexp = r"(\d+)\s+(...)"
# ndarray in output variable
output = np.fromregex('data.txt', regexp, [('num', np.int64), ('key', 'S3')])
print(output['num'])

Explanation

  • Lines 4–8: We use the 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.
  • 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 the data.txt file for the specified regexp criterion. This function has data.txt, regexp, and dtype as arguments and returns a NumPy ndarray.
  • Line 13: The output['num'] function only prints the numbers from the NumPy ndarray that was returned.

Free Resources