Python III (File
Python III (File
In Python, data abstraction can be achieved with the help of the abstract
classes and methods from the abc module.
o Abstract classes
o Abstract Method
Abstract Class
An abstract class in Python is a class that comprises at least one abstract
method and is not directly instantiable. Abstract methods are classes without
a body that defines common behaviors for derived classes. An abstract
method cannot exist without its parent class.
Example
1. from abc import ABC, abstractmethod
2.
3. # Abstract class
4. class Vehicle(ABC):
5.
6. @abstractmethod
7. def start(self):
8. pass # Abstract method with no implementation
9.
10. @abstractmethod
11. def stop(self):
12. pass
13.
14. # Concrete class implementing the abstract methods
15. class Car(Vehicle):
16.
17. def start(self):
18. print("Car is starting with a key ignition.")
19.
20. def stop(self):
21. print("Car is stopping using the brake.")
22.
23. # Trying to instantiate an abstract class will raise an error
24. # vehicle = Vehicle() # TypeError
25.
26. # Creating an instance of the concrete class
27. my_car = Car()
28. my_car.start()
29. my_car.stop()
Output:
Vehicle is an abstract class because it inherits ABC. The methods start() and
stop() are abstract. Any subclass is obligated to implement these methods.
The Car class actually implements these two methods. If one tries to create
an object of Vehicle directly, it will raise a TypeError.
Abstract Method
An abstract method in Python is a method declared in a base class, but lack
implementation. It serve as a placeholders that derived classes must
override. This method ensures a reliable interface across subclasses,
promising to provide their own implementation. We can define an abstract
method using the @abstractmethod decorator from the abc module.
Example
1. from abc import ABC, abstractmethod
2.
3. # Defining an abstract class
4. class Animal(ABC):
5.
6. @abstractmethod
7. def make_sound(self):
8. """Abstract method to be implemented by subclasses"""
9. pass
10.
11. # Concrete subclass implementing the abstract method
12. class Dog(Animal):
13.
14. def make_sound(self):
15. return "Bark!"
16.
17. # Concrete subclass implementing the abstract method
18. class Cat(Animal):
19.
20. def make_sound(self):
21. return "Meow!"
22.
23. # Trying to instantiate an abstract class will raise an error
24. # animal = Animal() # This will raise TypeError
25.
26. # Creating objects of concrete classes
27. dog = Dog()
28. cat = Cat()
29.
30. print(dog.make_sound())
31. print(cat.make_sound())
Output:
Bark!
Meow!
Explanation:
o public,
o protected, and
o private.
Let's explore each with examples.
Public Members
Public members can be accessed everywhere, inside the class, outside the
class, and inside derived (child) classes.
Example
1. class Car:
2. def __init__(self, brand, model):
3. [Link] = brand # Public attribute
4. [Link] = model # Public attribute
5.
6. def display(self):
7. print(f"Car: {[Link]} {[Link]}")
8.
9. # Creating an object
10. car = Car("Toyota", "Corolla")
11.
12. # Accessing public members
13. print([Link])
14. print([Link])
15.
16. # Calling public method
17. [Link]()
Output:
Toyota
Corolla
Car: Toyota Corolla
Explanation:
Public attributes (brand, model) will also be accessible outside the class. The
display() method which is also public can be accessed from other classes.
Protected Members
Protected members are indicated by a single underscore (_variable).
Example
1. class Car:
2. def __init__(self, brand, model, engine):
3. [Link] = brand # Public attribute
4. self._model = model # Protected attribute
5. self._engine = engine # Protected attribute
6.
7. def _show_details(self): # Protected method
8. print(f"Brand: {[Link]}, Model: {self._model}, Engine: {self._engine
}")
9.
10. class ElectricCar(Car):
11. def __init__(self, brand, model, battery_capacity):
12. super().__init__(brand, model, "Electric")
13. self.battery_capacity = battery_capacity
14.
15. def show_info(self):
16. self._show_details() # Accessing protected method from subclas
s
17. print(f"Battery: {self.battery_capacity} kWh")
18.
19. # Creating an object of ElectricCar
20. tesla = ElectricCar("Tesla", "Model S", 100)
21.
22. # Accessing protected members from subclass
23. tesla.show_info()
24.
25. # Accessing protected members outside the class (not recommended
)
26. print(tesla._model) # Works, but not recommended
Output:
Private Members
Private members are indicated by double underscores (__variable).
Example
1. class BankAccount:
2. def __init__(self, account_number, balance):
3. self.account_number = account_number # Public attribute
4. self.__balance = balance # Private attribute
5.
6. def get_balance(self): # Getter method
7. return self.__balance
8.
9. def set_balance(self, amount): # Setter method
10. if amount >= 0:
11. self.__balance = amount
12. else:
13. print("Invalid amount! Balance cannot be negative.")
14.
15. # Creating an account object
16. account = BankAccount("123456789", 1000)
17.
18. # Accessing public member
19. print(account.account_number) # Works fine
20.
21. # Trying to access private member directly (will raise AttributeError)
22. # print(account.__balance) # Uncommenting this will cause an error
23.
24. # Using getter method to access private attribute
25. print(account.get_balance()) # Works fine
26.
27. # Using setter method to update private attribute
28. account.set_balance(2000)
29. print(account.get_balance()) # Updated balance
30.
31. # Accessing private attribute using name mangling (Not recommend
ed)
32. print(account._BankAccount__balance) # Works, but should be avoid
ed
Output:
123456789
1000
2000
2000
Explanation:
Conclusion
Encapsulation hides the internal details and the implementation of the
object's attributes by preventing direct access. In Python, encapsulation is
applied through public, protected, and private members for class attributes,
and controlling access through getters and setters. It improves the security,
maintainability and structure of the code because of the convention-based
approach in Python.
Encapsulation Provides:
Private variables can be accessed with the help of setter and getter
methods, respectively.
Example
1. class BankAccount:
2. def __init__(self, balance):
3. self.__balance = balance
4.
5. def get_balance(self): # Getter
6. return self.__balance
7.
8. def set_balance(self, amount): # Setter
9. if amount >= 0:
10. self.__balance = amount
11. else:
12. print("Invalid amount!")
13.
14. account = BankAccount(1000)
15. print(account.get_balance())
16. account.set_balance(2000)
17. print(account.get_balance())
Output:
1000
2000
5. Can Private Members Be Accessed Outside the Class in Python?
Direct access is restricted making it impossible to avail the class attribute.
However, we can access Private Variable through name mangling.
(_ClassName__privateVar).
Next
Syntax
The syntax of the Lambda Function is given below -
Example
Here we share some examples of lambda functions in Python for learning
purposes. Program Code 1:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
10
Here we explain the above code. The lambda function is "lambda num:
num+4" in the given programme. The parameter is num, and the computed
and returned equation is num * 4.
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
10
Program Code 3:
1. a = lambda x, y : (x * y)
2. print(a(4, 5))
Output:
Now we compile the above code in python, and after successful compilation,
we run it. Then the output is given below -
20
Program Code 4:
Now we gave another example of a lambda function that adds 2 numbers
and return one result. The code is shown below -
1. a = lambda x, y, z : (x + y + z)
2. print(a(4, 5, 5))
Output:
Now we compile the above code in python, and after successful compilation,
we run it. Then the output is given below -
14
Program Code:
1. # Python code to show the reciprocal of the given number to highlight the
difference between def() and lambda().
2. def reciprocal( num ):
3. return 1 / num
4.
5. lambda_reciprocal = lambda num: 1 / num
6.
7. # using the function defined by def keyword
8. print( "Def keyword: ", reciprocal(6) )
9.
10. # using the function defined by lambda keyword
11. print( "Lambda keyword: ", lambda_reciprocal(6) )
Output:
Now we compile the above code in python, and after successful compilation,
we run it. Then the output is given below -
Introduction:
In this tutorial, we are discussing Python file handling. Python supports the
file-handling process. Till now, we were taking the input from the console and
writing it back to the console to interact with the user. Users can easily
handle the files, like read and write the files in Python. In another
programming language, the file-handling process is lengthy and complicated.
But we know Python is an easy programming language. So, like other things,
file handling is also effortless and short in Python.
Sometimes, it is not enough to only display the data on the console. The data
to be displayed may be very large, and only a limited amount of data can be
displayed on the console since the memory is volatile, it is impossible to
recover the programmatically generated data again and again.
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.
In Python, files are treated in two modes as text or binary. The file may be in
the text or binary format, and each line of a file is ended with the special
character like a comma (,) or a newline character. Python executes the code
line by line. So, it works in one line and then asks the interpreter to start the
new line again. This is a continuous process in Python.
o Open a file
o Read or write - Performing operation
o Close the file
Opening a file
A file operation starts with the file opening. At first, open the File then Python
will start the operation. File opening is done with the open() function in
Python. This function will accepts two arguments, file name and access mode
in which the file is accessed. When we use the open() function, that time we
must be specified the mode for which the File is opening. The function
returns a file object which can be used to perform various operations like
reading, writing, etc.
Syntax:
Let's look at the simple example to open a file named "[Link]" (stored in the
same directory) in read mode and printing its content on the console.
It is a read operation in Python. We open an existing file with the given code
and then read it. The code is given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
<class '_io.TextIOWrapper'>
file is opened successfully
In the above code, we have passed filename as a first argument and
opened file in read mode as we mentioned r as the second argument.
The fileptr holds the file object and if the file is opened successfully, it will
execute the print statement
1. file = open('[Link]','w')
2. [Link]("Here we write a command")
3. [Link]("Hello users of JAVATPOINT")
4. [Link]()
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
1. > Hi
2. ERROR!
3. Traceback (most recent call last):
4. File "<stdin>", line 1, in <module>
5. NameError: name 'Hi' is not defined
We can perform any operation on the file externally using the file system
which is the currently opened in Python; hence it is good practice to close
the file once all the operations are done. Earlier use of the close() method
can cause the of destroyed some information that you want to write in your
File.
Syntax
1. [Link]()
Consider the following example.
Here we write the program code for the closing method in Python. The code
is given below -
1. try:
2. fileptr = open("[Link]")
3. # perform file operations
4. finally:
5. [Link]()
Syntax:
Here we write the program code for with statement in Python. The code is
given below -
1. with open("[Link]",'r') as f:
2. content = [Link]();
3. print(content)
Program code 2 for with statement:
Here we write the program code for with statement in Python. The code is
given below -
w: It will overwrite the file if any file exists. The file pointer is at the
beginning of the file.
a: It will append the existing file. The file pointer is at the end of the file. It
creates a new file if no file exists.
Here we write the program code for write method in Python. The code is
given below -
1. # open the [Link] in append mode. Create a new file if no such file exists.
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
[Link]
Python is the modern programming language. It is done any kind of program
in shortest way.
We have opened the file in w mode. The [Link] file doesn't exist, it created
a new file and we have written the content in the file using the write()
function
Here we write the program code for write method in Python. The code is
given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Hello coders
Welcome to javaTpoint
Program code 3 for Write Method:
Here we write the program code for write method in Python. The code is
given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Syntax:
1. [Link](<count>)
Here, the count is the number of bytes to be read from the file starting from
the beginning of the file. If the count is not specified, then it may read the
content of the file until the end.
Here we write the program code for read() method in Python. The code is
given below -
1. #open the [Link] in read mode. causes error if no such file exists.
2. fileptr = open("[Link]","r")
3. #stores all the data of the file into the variable content
4. content = [Link](10)
5. # prints the type of the data stored in the file
6. print(type(content))
7. #prints the content of the file
8. print(content)
9. #closes the opened file
10. [Link]()
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
<class 'str'>
Python is
In the above code, we have read the content of [Link] by using
the read() function. We have passed count value as ten which means it will
read the first ten characters from the file.
If we use the following line, then it will print all content of the file. So, it only
prints 'Python is'. For read the whole file contents, the code is given below -
1. content = [Link]()
2. print(content)
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Here we give an example of read file using for loop. The code is given below
-
1. #open the [Link] in read mode. causes an error if no such file exists.
2. fileptr = open("[Link]","r");
3. #running a for loop
4. for i in fileptr:
5. print(i) # i contains each line of the file
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Here we give an example of read file using for loop. The code is given below
-
Line1: H
Line2: e
Line3: l
Line4: l
Line5: o
Line6:
Line7: C
Line8: o
Line9: d
Line10: e
Line11: r
Line12: s
Line13:
Line14: J
Line15: a
Line16: v
Line17: a
Line18: T
Line19: p
Line20: o
Line21: i
Line22: n
Line23: t
Line24:
Here we give the example of reading the lines using the readline() function in
Python. The code is given below -
1. #open the [Link] in read mode. causes error if no such file exists.
2. fileptr = open("[Link]","r");
3. #stores all the data of the file into the variable content
4. content = [Link]()
5. content1 = [Link]()
6. #prints the content of the file
7. print(content)
8. print(content1)
9. #closes the opened file
10. [Link]()
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Example 2:
Here we give the example of reading the lines using the readline() function in
Python. The code is given below -
1. #open the [Link] in read mode. causes error if no such file exists.
2. fileptr = open("[Link]","r");
3.
4. #stores all the data of the file into the variable content
5. content = [Link]()
6.
7. #prints the content of the file
8. print(content)
9.
10. #closes the opened file
11. [Link]()
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Here we give the example of reading the lines using the readline() function in
Python. The code is given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Line1: Hello
Line2: Coders
Line3: JavaTpoint
x, a and w is the modes of open() function. The uses of these modes are
given below -
x: it creates a new file with the specified name. It causes an error a file
exists with the same name.
a: It creates a new file with the specified name if no such file exists. It
appends the content to the file if the file already exists with the specified
name.
w: It creates a new file with the specified name if no such file exists. It
overwrites the existing file.
Here we give an example for creating a new file in Python. For creates a file,
we have to used the open() method. The code is given below -
1. #open the [Link] in read mode. causes error if no such file exists.
2. fileptr = open("[Link]","x")
3. print(fileptr)
4. if fileptr:
5. print("File created successfully")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Here we give an example for creating a new file in Python. For creates a file,
we have to use the open() method. Here we use try block for erase the
errors. The code is given below -
1. try:
2. with open('[Link]', 'w') as f:
3. [Link]('Here we create a new file')
4. except FileNotFoundError:
5. print("The file is does not exist")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
1. [Link]()
Program code1 for File Pointer Position:
Here we give an example for how to find file pointer position in Python. Here
we use tell() method and it is return byte number. The code is given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
For this purpose, the Python provides us the seek() method which enables us
to modify the file pointer position externally. That means, using seek()
method we can easily change the cursor in the file, from where we want to
read or write a file.
Syntax:
1. <file-ptr>.seek(offset[, from)
The seek() method accepts two parameters:
offset: It refers to the new position of the file pointer within the file.
from: It indicates the reference position from where the bytes are to be
moved. If it is set to 0, the beginning of the file is used as the reference
position. If it is set to 1, the current position of the file pointer is used as the
reference position. If it is set to 2, the end of the file pointer is used as the
reference position.
Here we give the example of how to modifying the pointer position using
seek() method in Python. The code is given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Python OS module:
Renaming the file
The Python os module enables interaction with the operating system. It
comes from the Python standard utility module. The os module provides a
portable way to use the operating system-dependent functionality in Python.
The os module provides the functions that are involved in file processing
operations like renaming, deleting, etc. It provides us the rename() method
to rename the specified file to a new name. Using the rename() method, we
can easily rename the existing File. This method has not any return value.
The syntax to use the rename() method is given below.
Syntax:
1. rename(current-name, new-name)
The first argument is the current file name and the second argument is the
modified name. We can change the file name bypassing these two
arguments.
1. import os
2.
3. #rename [Link] to [Link]
4. [Link]("[Link]","[Link]")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Here we give an example of the renaming of the files using rename() method
in Python. The current file name is the source, and the new file name is the
destination. The code is given below -
1. import os
2. def main():
3. i=0
4. path="D:/JavaTpoint/"
5. for filename in [Link](path):
6. destination = "new" + str(i) + ".png"
7. source = path + filename
8. destination = path + destination
9. [Link](source, destination)
10. i += 1
11.
12. if __name__ == '__main__':
13. main()
Syntax:
1. remove(file-name)
Program code 1 for remove() method:
1. import os;
2. #deleting the file named [Link]
3. [Link]("[Link]")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
1. import os
2. if [Link]("[Link] "):
3. [Link]("[Link] ")
4. else:
5. print("This file is not existed")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Syntax:
path - A path like object represent a path either bytes or the strings object.
dir_fd - When the specified path is absolute, in that case dir_fd is ignored. Its
use is optional in mkdir() method.
Here we give the example of mkdir() method by which we can create new
dictionary in Python. The code is given below -
1. import os
2.
3. #creating a new directory with the name new
4. [Link]("new")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Here we give the example of mkdir() method by which we can create new
dictionary in Python. The code is given below -
1. import os
2. path = '/D:/JavaTpoint'
3. try:
4. [Link](path)
5. except OSError as error:
6. print(error)
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Syntax
1. [Link]()
Program code 1 for getcwd() Method:
Here we give the example of getcwd() method by which we can create new
dictionary in Python. The code is given below -
1. import os
2. [Link]()
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
'C:\\Users\\DEVANSH SHARMA'
Program code 2 for getcwd() Method:
Here we give the example of getcwd() method by which we can create new
dictionary in Python. The code is given below -
1. import os
2. c = [Link]()
3. print("The working directory is:", c)
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Syntax
1. chdir("new-directory")
Program code 1 for chdir() Method:
Here we give the example of chdir() method by which we can change the
current working dictionary into new dictionary in Python. The code is given
below -
1. import os
2. # Changing current directory with the new directiory
3. [Link]("C:\\Users\\DEVANSH SHARMA\\Documents")
4. #It will display the current working directory
5. [Link]()
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
'C:\\Users\\DEVANSH SHARMA\\Documents'
Program code 2 for chdir() Method:
1. import os
2. [Link](r"C:\Users\JavaTpoint")
3. print("Currently working directory is changed")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Deleting directory:
The rmdir() method is used to delete the specified directory. If the directory
is not empty then there is occurs OSError. The rmdir() method does not have
and kind of return value.
Syntax
1. [Link](directory name)
Program code 1 for rmdir() Method:
1. import os
2. #removing the new directory
3. [Link]("directory_name")
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
1. import os
2. directory = "JavaTpoint"
3. parent = "/D:/User/Documents"
4. path = [Link](parent, directory)
5. [Link](path)
6. print("The directory '%s' is successfully removed", %directory)
Output:
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
Now we compile the above code in Python, and after successful compilation,
we run it. Then the output is given below -
The following example contains two python scripts. The script [Link]
executes the script [Link] and writes its output to the text file [Link].
Program code:
[Link]
1. temperatures=[10,-20,-289,100]
2. def c_to_f(c):
3. if c< -273.15:
4. return "That temperature doesn't make sense!"
5. else:
6. f=c*9/5+32
7. return f
8. for t in temperatures:
9. print(c_to_f(t))
[Link]
1. import subprocess
2.
3. with open("[Link]", "wb") as f:
4. subprocess.check_call(["python", "[Link]"], stdout=f)
S Method Description
N
7 [Link]([size]) It reads one line from the file and places the
file pointer to the beginning of the new line.
The rows in the CSV document are the data log where each log is composed
of one or more fields, divided by commas. CSV is one of the popular file
formats used for importing and exporting the spreadsheets and databases.
In Python, we can read CSV files using the functions of built-in csv
module and the pandas library. In the following sections, we will explore
the different ways to read CSV files in Python.
File: [Link]
1. Organization,Industry,Employees
2. Google,IT,1500
3. Microsoft,IT,1300
4. Tata,Manufacturing,1000
5. Tpoint Tech,Education,200
6. Apple,IT,1200
In the above CSV file, we have some values separated by commas ','. We will
be using this file for the examples in the following sections.
o csv Module: The csv module is one of the built-in modules in Python.
It offers various classes and functions that help us read and write
tabular information in CSV file format.
To use csv module in Python, we need to import it. The following syntax will
guide us how to use the csv module for our purpose.
Syntax:
1. import csv
Syntax:
1. import pandas
1) Using [Link]()
[Link]() is a function provided by the built-in csv module in Python. It is
used to read data from a CSV file. It returns a reader object, which can be
utilized in order to iterate over lines in the given CSV file.
Let us see an example of the [Link]() function.
Example
1. import csv
2.
3. # opening the CSV file
4. with open('[Link]', newline='') as file:
5. # using the reader() function to read the content of the file
6. reader = [Link](file)
7.
8. # printing each row of the table
9. for row in reader:
10. print(row)
Output:
In the above example, we imported the csv module and used the
'with open()' statement to open the [Link] file. We have then used
the [Link]() function in order to read the content from the CSV file. At
last, we printed the file content row-wise.
2) Using [Link]()
With the help of the [Link]() class, we can convert the CSV files into
dictionaries where field names work as keys. The filesystem offers us with
simple value field accessibility that makes the data more readable. This class
returns row data as dictionaries when running an iteration process.
Example
1. import csv
2.
3. # opening the CSV file
4. with open('[Link]', newline='') as file:
5. # using the DictReader() class to convert the content of the file into a d
ictionary
6. reader = [Link](file)
7.
8. # printing each row of the table
9. for row in reader:
10. print(row)
Output:
In the above example, we have imported the csv module. We then used the
'with open()' statement to open the CSV file. After that, we have used the
DictReader() class from the csv module to turn each rows into a dictionary
through its column headers. At last, we printed the data in the dictionary
format.
3) Using pandas.read_csv()
Pandas, which uses NumPy, is an open-source library people use to easily
work with and analyze data. It helps with simplifying all these data handling
steps. The read_csv() function allows us to load CSV files as DataFrames,
helps save time and manage the information in a structured way.
Example
1. import pandas as pd
2.
3. # Reading the CSV file into a DataFrame
4. dframe = pd.read_csv('[Link]')
5.
6. # Displaying the DataFrame
7. print(dframe)
Output:
Here, we have imported the pandas library. We then used the read_csv()
function to convert the given CSV file into the DataFrame and stored it in a
variable. At last, we used the print statement to print DataFrame.
As a result, the CSV file is converted into rows and columns. This structure
makes it easier for us to understand the information from the CSV file.
Conclusion
Python is a versatile and powerful programming language that offers various
methods to read CSV files. In this tutorial, we learned some of these
methods. We discovered how the csv module and the pandas library help us
read CSV files. We learned about the different functions and classes of the
csv module that allow us to read the CSV files into different formats.
Similarly, the read_csv() function of the pandas library allow us view the CSV
file into the dictionary format.