The genfromtxt()
function is used to load data in a program from a text file. It takes multiple argument values to clean the data of the text file. It also has the ability to deal with missing or null values through the processes of filtering, removing, and replacing.
Note: The
genfromtxt()
function from the Numpy module is perfect for data loading and cleaning.
# Signature according to documentationnumpy.genfromtxt(fname,dtype= <class 'float'>,comments= '#',delimiter= None,skip_header= 0,skip_footer= 0,converters= None,missing_values= None,filling_values= None,usecols= None,names= None,excludelist= None,deletechars= "!#$%&'()*+, -./:;<=>?@[\\]^{|}~",replace_space= '_',autostrip= False,case_sensitive= True,defaultfmt= 'f%i',unpack= None,usemask= False,loose= True,invalid_raise= True,max_rows= None,encoding= 'bytes',*,like= None)
There are numerous argument values for the genfromtxt()
function. However, in this shot, we'll only focus on the most common ones:
fname
: {generator, list of strings, path-like object, file}dtype
: {data type}dtype
is 'float'
.comments='#'
: {string}delimiter=None
: {sequence, string, integer}skip_header=0
: {integer value}skip_footer=0
: {integer value}converters=None
: {variable maybe lambdas, etc.}missing_values=None
: {variable maybe a string, etc.}filling_values=None
: {variable maybe a string, etc.}usecols=None
: {sequence of integers, column name, etc.}replace_space='_'
: {char value}max_rows=None
: {integer value}encoding='bytes'
: {string}like
: {string_like object}ndarray
: This function returns data as an array. If usemask
is set, it returns a masked array.
# load numpy libraryimport numpy as np# invoking genfromtxt method to read employee.txt filecontent = np.genfromtxt("employee.txt", dtype=str, encoding = None, delimiter=",")# print file data on consoleprint("File data:", content)
genfromtxt()
function to load data from the employee.txt
file. The dtype=str
data type loads the word after the comma delimiter as a Python string and returns all of the file data as an ndarray
array.ndarray
array.