Download as pdf or txt
Download as pdf or txt
You are on page 1of 109

Unit IV

File Handling
 Python too supports file handling and allows users to
handle files i.e., to read and write files, along with many
other file handling options, to operate on files.
 The file handling plays an important role when the data
needs to be stored permanently into the file. A file is a
named location on disk to store related information.
 We can access the stored information (non-volatile) after
the program termination.
Types of Files in Python

 Text file,
 Binary Files,
 CSV file
Text file
 Text file store the data in the form of characters.
 Text file are used to store characters or strings.
 Usually we can use text files to store character data
 eg: abc.txt
Binary file
Binary file store entire data in the form of bytes.
Binary file can be used to store text, image,
audio and video.
Usually we can use binary files to store binary
data like images,video files, audio files etc
CSV File
 CSV (Comma Separated Values) is a simple file format used to store
tabular data, such as a spreadsheet or database.
 A CSV file stores tabular data (numbers and text) in plain text. Each
line of the file is a data record.
 Each record consists of one or more fields, separated by commas.
 The use of the comma as a field separator is the source of the name
for this file format.
 For working CSV files in Python, there is an inbuilt module called
csv.
Working of open() function
 Before performing any operation (like read or write) on the
file, first we have to open that file. For this we should use
Python's inbuilt function open()
 But at the time of open, we have to specify mode, which
represents the purpose of opening file.
 We should use open() function to open a file. This function
accepts 'filename' and 'open mode' in which to open the file.
 file handler = open("file name", "open mode", "buffering")
 f = open(filename, mode)
The File Opening Mode
w
 To write data into file. If any data is already present in the file, it would be
deleted and the present data will be stored.
 open an existing file for write operation. If the file already contains some
data then it will be overridden. If the specified file is not already available
then this mode will create that file.
 r
 To read the data form the file. The file pointer is positioned at the
beginning of the file.
 open an existing file for read operation. The file pointer is positioned at
the beginning of the file. If the specified file does not exist then we will
get FileNotFoundError. This is default mode.
The File Opening Mode
a
 To append data to the file. Appending means adding at the end of
existing data. The file pointer is placed at the end of the file. If the
file does not exist, it will create new for writing data.
 open an existing file for append operation. It won't override existing
data.If the specified file is not already avaialble then this mode will
create a new file.
 w+
 To write and read data a file. The previous data in the file will be
deleted.
 To write and read data. It will override existing data.
The File Opening Mode
 r+
 To read and write data into the file. The previous data in the file will not
be deleted.The file pointer is placed at the beginning of the file.
 a+
 To append and read data from the file.It wont override existing data.
 To append and read of a file. The file pointer will be at the end of the file if
the file exists. If the file does not exist, it creates a new file for reading and
writing.
 x
 To open a file in exclusive creation mode for write operation. If the file
already exists then we will get FileExistsError.
The File Opening Mode
 All the above modes are applicable for text files. If the above
modes suffixed with 'b' then these represents for binary
files.
 Eg: rb,wb,ab,r+b,w+b,a+b,xb
 f = open("abc.txt","w")
 We are opening abc.txt file for writing data.
Working of open() function
 Before performing any operation on the file like reading or writing, first,
we have to open that file.
 For this, we should use Python’s inbuilt function open() but at the time of
opening, we have to specify the mode, which represents the purpose of
the opening file.
 f = open(filename, mode)
 a=open("D:\\new.txt","r")
 for b in a:
print(b)
 The open command will open the file in the read mode and the for loop
will print each line present in the file.
Working of open() function
Working of read() mode

 There is more than one way to read a file in Python.


 If you need to extract a string that contains all characters in the file then
we can use file.read().
There is more than one way to read a file in Python. If you need to
extract a string that contains all characters in the file then we can
use file.read().
Another way to read a file is to call a certain number of characters
like in the following code the interpreter will read the first five
characters of stored data and return it as a string:
Creating a file using write() mode
 In Python, you use the open() function with one of the following options –
"x" or "w" – to create a new file:

 "x" – Create: this command will create a new file if and only if there is no
file already in existence with that name or else it will return an error.
 We've now created a new empty text file!
 But if you retry the code below – for example, if you try to create a new file
with the same name as you used (if you want to reuse the filename above)
you will get an error notifying you that the file already exists.
Creating a file using write() mode
 "w" – Write: this command will create a new text file
whether or not there is a file in the memory with the new
specified name.
 It does not return an error if it finds an existing file with the
same name – instead it will overwrite the existing file.
Creating a file using write() mode

The close() command terminates all the resources in use and frees
the system of this particular program.
Creating a file using write() mode
 With the code above, whether the file exists or the file doesn't exist in the
memory, you can still go ahead and use that code. Just keep in mind that it
will overwrite the file if it finds an existing file with the same name.
How to Write to a File in Python
 There are two methods of writing to a file in Python, which
are:
 The write() method:
 This function inserts the string into the text file on a single
line.
 Based on the file we have created above, the below line of
code will insert the string into the created text file, which is
"myfile.txt.”
 file.write("Hello There\n")
How to Write to a File in Python
 The writelines() method:
 This function inserts multiple strings at the same time. A
list of string elements is created, and each string is then
added to the text file.
 Using the previously created file above, the below line of
code will insert the string into the created text file, which is
"myfile.txt.”
 f.writelines(["Hello World ", "You are welcome to Fcc\n"])
Working of append() mode
Closing a file
 Once we are done with the read/write operations on a file, it is a good
practice to close the file.
 Python provides a close() method to do so.
 While closing a file, the system frees the memory allocated to it.
 The syntax of close() is:
 file_object.close()
 Here, file_object is the object that was returned while opening the file.
 Python makes sure that any unwritten or unsaved data is flushed off
(written) to the file before it is closed.
 Hence, it is always advised to close the file once our work is done.
 Also, if the file object is re-assigned to some other file, the previous file is
automatically closed.
NumPy Library
 What is NumPy?
 NumPy is a Python library used for working with arrays.
 It also has functions for working in domain of linear algebra,
fourier transform, and matrices.
 NumPy was created in 2005 by Travis Oliphant. It is an open
source project and you can use it freely.
 NumPy stands for Numerical Python.
NumPy Library
 Why Use NumPy?
 In Python we have lists that serve the purpose of arrays, but they are
slow to process.
 NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
 The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.
 Arrays are very frequently used in data science, where speed and
resources are very important.
NumPy Library
 Why is NumPy Faster Than Lists?
 NumPy arrays are stored at one continuous place in memory unlike
lists, so processes can access and manipulate them very efficiently.
 This behavior is called locality of reference in computer science.
 This is the main reason why NumPy is faster than lists. Also it is
optimized to work with latest CPU architectures.
 Which Language is NumPy written in?
 NumPy is a Python library and is written partially in Python, but
most of the parts that require fast computation are written in C or
C++.
Creation of One-Dimensional Arrays
 One dimensional array contains elements only in one dimension.
In other words, the shape of the NumPy array should contain only
one value in the tuple.
 Method 1: First make a list then pass it in numpy.array()
 Method 2: fromiter() is useful for creating non-numeric sequence
type array however it can create any type of array. Here we will
convert a string into a NumPy array of characters.
 Method 3: arange() returns evenly spaced values within a given
interval.
 Method 4: linspace() creates evenly space numerical elements
between two given limits.
Method 1: First make a list then pass it in numpy.array()
Method 2: fromiter()
Method 3: arange()
Method 3: linespace
Re-shaping of an Array
 Numpy is the fundamental package for scientific computing
with Python. Numpy is basically used for creating array of n
dimensions.
 Reshaping numpy array simply means changing the shape
of the given array, shape basically tells the number of
elements and dimension of array, by reshaping an array we
can add or remove dimensions or change number of
elements in each dimension.
 In order to reshape a numpy array we use reshape method
with the given array.
Re-shaping of an Array
Reshaping : 1-D to 2D
Reshaping : 1-D to 3-D
Reshaping N-D to 1-D array
Element-wise Operations on Matrix (Array)
 Operation on Matrix :
 1. add() :- This function is used to perform element wise matrix addition.
 2. subtract() :- This function is used to perform element wise matrix
subtraction.
 3. divide() :- This function is used to perform element wise matrix division.
 4. multiply() :- This function is used to perform element wise matrix
multiplication.
 5. dot() :- This function is used to compute the matrix multiplication, rather
than element wise multiplication.
 6. sqrt() :- This function is used to compute the square root of each
element of matrix.
 7. sum(x,axis) :- This function is used to add all the elements in matrix.
Optional “axis” argument computes the column sum if axis is 0 and row sum
if axis is 1.
 8. “T” :- This argument is used to transpose the specified matrix.
NumPy Array Indexing

 Array indexing is the same as accessing an array element.


 You can access an array element by referring to its index number.
 The indexes in NumPy arrays start with 0, meaning that the first element
has index 0, and the second has index 1 etc.
Negative Indexing
NumPy Array Slicing
 Slicing in python means taking elements from one given index
to another given index.
 We pass slice instead of index like this: [start:end].
 We can also define the step, like this: [start:end:step].
 If we don't pass start its considered 0
 If we don't pass end its considered length of array in that
dimension
 If we don't pass step its considered 1
Negative Slicing
STEP in Slicing
Slicing 2-D Arrays
Add Row to NumPy Array in Python

 1. Using append() method to Add a Row to a NumPy Array


 2. Using concatenate() method to Add a Row to a NumPy Array
 3. Using insert() method to Add a Row to a NumPy Array
 4. Using vstack() method to Add a Row to a NumPy Array
Using append() method to Add a Row to a
NumPy Array
 Numpy module in python, provides a function numpy.append() to
append objects to the end of an array, The object should be an array like
entity.
 The append() method will take an array, object to be appended as
arguments.
 It returns a copy of the numpy array, with given values appended at the
end.
Using append() method to Add a Row to a
NumPy Array
Using concatenate() method to Add a Row to a NumPy
Array

 Numpy module in python, provides a function


numpy.concatenate() to join a sequence of arrays along an
existing axis.
 The concatenate() method will take a sequence of arrays as
parameters.
 It will concatenate the arrays into one single array and
returns the concatenated array.
Using concatenate() method to Add a Row to a NumPy
Array
Using insert() method to Add a Row to a NumPy
Array
 Numpy module in python, provides a function
numpy.insert() to insert values along the given axis
before the given index.
 The insert() method will take an array, index , values to
be inserted as parameters.
 It will insert the given value just before the specified
index and returns the array.
Using insert() method to Add a Row to a NumPy
Array
Using vstack() method to Add a Row to a NumPy
Array
 Numpy module in python, provides a function numpy.vstack()
function is used to Stack arrays in sequence vertically (row-wise).
i.e, concatenating into a single array.
 The vstack() method will take a sequence of arrays as parameters.
 It will stack the arrays into one single array and returns the array.
 The vstack is equivalent to concatenation.
Using vstack() method to Add a Row to a NumPy
Array
Add Columns to NumPy Array in Python
 1.) Using append() method to Add a Column to a NumPy Array
 2.) Using concatenate() method to add a column to NumPy Array
 3.) Using insert() method to add a column to NumPy Array
 4.) Using column_stack() to add a column to 2D NumPy Array
 5.) Using hstack() to add a column to 2D NumPy Array

Using append() method to Add a Column to a NumPy
Array
 Numpy module in python, provides a function numpy.append()
to add objects to the end of an array.
 The object should be an array like entity.
 The append() method will take an array and object to be
appended as arguments.
 It will append the given object at the end of the copy of given
array and returns the new array with the appended data.
Using append() method to Add a Column to a NumPy
Array
Using concatenate() method to add a column to NumPy
Array

 The Numpy module in python, provides a function


numpy.concatenate() to join a sequence of arrays along an
existing axis.
 The concatenate() method will take a sequence of arrays as
parameters.
 It will concatenate the arrays into one single array and
returns the concatenated array.
Using concatenate() method to add a column to NumPy
Array
Using insert() method to add a column to NumPy
Array
 The Numpy module in python, provides a function numpy.insert() to
insert values along the given axis before the given index. The insert()
method will take an array, an index and values to be inserted as
parameters. It will insert the given value just before the specified index in
a copy of array and returns that modified copy of array.
 Now, to add a column to a NumPy Array, we need to pass the array, index,
and values to be inserted as column to the insert() method and also set
the axis = 1.
Using insert() method to add a column to NumPy
Array
Using column_stack() to add a column to 2D NumPy
Array

 Numpy module in python, provides a


function numpy.column_stack() function is used to stack arrays in Column-
wise. i.e, The column_stack() method will take a sequence of 1-D arrays and
stack them as columns to make a single 2-D array and returns the array.
 The column_stack() is similar to h_stack(). The column_stack() method first
converts the 1-D arrays passed to it into 2-D columns.
 Now to add a column to a NumPy Array, in the sequence, we will pass the given
array and the column to be added to the column_stack() method.
 It will return the array with the column added.
Using column_stack() to add a column to 2D NumPy
Array
Using hstack() to add a column to 2D NumPy
Array
 Numpy module in python, provides a function numpy.hstack() function
is used to stack the sequence of input arrays horizontally i.e,
concatenating into a single array. The hstack() method will take a
sequence of arrays as parameters. It will concatenate the arrays into one
single array and returns the array. The hstack() is equivalent to
concatenation.
 Now to add a column to a NumPy Array, in the sequence of arrays, pass the
given array and the column to be added. The hstack() method will return
the array with the column added.
Using hstack() to add a column to 2D NumPy
Array
Array Manipulation
 Several routines are available in NumPy package for
manipulation of elements in ndarray object.
 This topic present the functions of Basic operations,
Changing array shape, Transpose-like operations, Changing
number of dimensions, Changing kind of array, Joining
arrays, Splitting arrays, Tiling arrays, Adding and removing
elements and Rearranging elements to access data and
subarrays, and to split, reshape, and join the arrays.
flatten()-this method is used to flatten array elements.
ravel()-this method is used to flatten array elements.
Flatten Vs Ravel
Numpy flatten() Numpy ravel()
flatten() function returns ravel() function simply returns
a flattened copy of Numpy array. a flattened view of Numpy array.

Changes made to flattened array If you try to modify the flattened


is not reflected back to the view then you end up with that
original array. same change in the original
array.
flatten() occupies memory so it is ravel() does not occupy memory
considered slower so we can say that it is faster
than ravel() function. than flatten()
transpose()- used to transpose a matrix
concatenate()- this method is used to concatenate 2 arrays.
stack()-This function joins the sequence of arrays along a
new axis.
hstack() and vstack()- hstack stacks arrays in sequence
horizontally (column wise) and vstack stacks arrays in
sequence horizontally (column wise)
split()-divides the array into sub arrays along a specified
axis.
hsplit() and vsplit(): these methods are used to split array
in horizontal and vertical manner.
delete()-this method is used to delete specific element,
row or column.
unique()-this method is used to find unique values from an
array.
Introduction to matplotlib
 Matplotlib is one of the most popular Python packages used for data
visualization.
 It is a cross-platform library for making 2D plots from data in arrays.
 Matplotlib is written in Python and makes use of NumPy, the numerical
mathematics extension of Python.
 It provides an object-oriented API that helps in embedding plots in
applications using Python GUI toolkits such as PyQt,
WxPythonotTkinter.
 It can be used in Python and IPython shells, Jupyter notebook and web
application servers also.
Introduction to Matplotlib
 Matplotlib is a low level graph plotting library in python
that serves as a visualization utility.
 Matplotlib was created by John D. Hunter.
 Matplotlib is open source and we can use it freely.
 Matplotlib is mostly written in python, a few segments are
written in C, Objective-C and JavaScript for Platform
compatibility.
Basic plots in Matplotlib

 Matplotlib comes with a wide variety of plots.


 Plots helps to understand trends, patterns, and to
make correlations.
 They’re typically instruments for reasoning about
quantitative information.
Line plot
Bar plot
Histogram
Scatter Plot
Pie Charts
Pie Charts
Pie Charts
Pie Charts

You might also like