A CSV file is a file that contains data in the form of characters separated by commas. CSV files are often used t o store and exchange data between different applications that can handle tabular data , such as spreadsheets, databases, contact managers, etc

The csv module is a built-in module that provides functions and classes for reading and writing data in CSV format. CSV stands for comma-separated values, which is a common format for storing and exchanging tabular data, such as spreadsheets and databases. 

To use the csv module, we need to import it first:

import csv

Opening a CSV file

To open a CSV file in Python, we use the same open() function that we use for text files. However, we need to specify the newline parameter as an empty string to avoid any extra newlines in the data

For example, to open a CSV file named “people.csv” in read mode, we can write:

f = open("people.csv", "r", newline="")

Closing a CSV file

To close a CSV file in Python, we use the same close() method that we use for text files. The close() method releases the resources associated with the file and ensures that any changes made to the file are saved. It is important to close a file after using it to avoid errors or data loss.  

For example, to close the file object f, we can write:

f.close()

Writing to a CSV file

To write data into a CSV file in Python, we use the csv.writer() function to create a writer object that can write data to the file. The writer object has methods like writerow() and writerows() that can write one or more rows of data to the file. 

For example, to write some data to a CSV file named “people.csv” in write mode, we can write:

import csv

# open a CSV file in write mode

f = open("people.csv", "w", newline="")

# create a writer object

writer = csv.writer(f)

# write the header row

writer.writerow(["name", "id", "age", "gender"])

# write some data rows

writer.writerow(["Alice", "001", 25, "F"])

writer.writerow(["Bob", "002", 30, "M"])

writer.writerow(["Charlie", "003", 35, "M"])

# write multiple data rows at once

data = [

    ["David", "004", 40, "M"],

    ["Eve", "005", 45, "F"],

    ["Frank", "006", 50, "M"]

]

writer.writerows(data)

# close the file

f.close()

Reading from a CSV file

To read data from a CSV file in Python, we use the csv.reader() function to create a reader object that can iterate over the lines in the file . The reader object returns each line as a list of strings. 

For example, to read some data from a CSV file named “people.csv” in read mode, we can write:

import csv

# open a CSV file in read mode

f = open("people.csv", "r", newline="")

# create a reader object

reader = csv.reader(f)

# loop over each line in the file

for row in reader:

    # print the row as a list of strings

    print(row)

# close the file

f.close()

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo