How to read CSV file in Python?

You can read a CSV file in Python using the built-in csv module. Here’s an example of how to do it:

import csv

with open('example.csv') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)

In this example, we first import the csv module. We then open the CSV file using the open function and pass it to csv.reader. We use a with block to automatically close the file when we’re done reading it.

Once we have the reader object, we can iterate over it using a for loop. Each row of the CSV file is returned as a list, and we print it out in this example.

You can also access individual columns of the CSV file by indexing into the row list. For example, row[0] would give you the first column of the current row.