Working with Python’s csv module
The csv
module from Python allows us to interact with files that are in CSV format, which is nothing but a text file format – that is, the data stored inside the CSV files is human-readable.
The csv
module requires that the file is opened before the methods supplied by the csv
module can be applied. Let’s take a look at how we can start with the very basic operation of reading data from CSV files.
Reading data from a CSV file
Reading data from CSV files is quite easy and consists of the following steps:
First, we must open the file by running the following script:
csv_file = open('path to csv file')
Here, we are reading the file using the Python open()
method, which requires the fully qualified name of the file. This should be opened so that it can be passed as the parameter to the method.
Then, we must read the data from the file
object using the csv
module’s reader
method:
import csv csv_data...