Read and Write CSV files (2024)

The CSV (Comma Separated Values) format is a common and straightforward way to store tabular data. To represent a CSV file, it should have the .csv file extension.

Now, let's proceed with an example of the info .csv file and its data.

SN, Name, City1, Michael, New Jersey2, Jack, California

Working With CSV Files in Python

Python provides a dedicated csv module to work with csv files. The module includes various methods to perform different operations.

However, we first need to import the module using:

import csv

Read CSV Files with Python

The csv module provides the csv.reader() function to read a CSV file.

Suppose we have a csv file named people.csv with the following entries.

Name, Age, ProfessionJack, 23, DoctorMiller, 22, Engineer

Now, let's read this csv file.

import csvwith open('people.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row)

Output

['Name', 'Age', 'Profession']['Jack', '23', 'Doctor']['Miller', '22', 'Engineer']

Here, we have opened the people.csv file in reading mode using:

with open(airtravel.csv', 'r') as file:

We then used the csv.reader() function to read the file. To learn more about reading csv files, Python Reading CSV Files.

Using csv.DictReader() for More Readable Code

The csv.DictReader() class can be used to read the CSV file into a dictionary, offering a more user-friendly and accessible method.

Suppose we want to read the following people.csv file.

Name, Age, ProfessionJack, 23, DoctorMiller, 22, Engineer

Now let's read this file.

import csvwith open('people.csv', 'r') as file: csv_file = csv.DictReader(file) for row in csv_file: print(row)

Output

{'SN': '1', ' Name': ' Michael', ' City': ' New Jersey'}{'SN': '2', ' Name': ' Jack', ' City': ' California'}

In this example, we have read data from the people.csv file and print each row as a dictionary.

Here, we used csv.DictReader(file), which treats the first row of the CSV file as column headers and each subsequent row as a data record.

Write to CSV Files with Python

The csv module provides the csv.writer() function to write to a CSV file.

Let's look at an example.

import csvwith open('protagonist.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerow(["SN", "Movie", "Protagonist"]) writer.writerow([1, "Lord of the Rings", "Frodo Baggins"]) writer.writerow([2, "Harry Potter", "Harry Potter"])

When we run the above program, a protagonist.csv file is created with the following content:

SN,Movie,Protagonist1,Lord of the Rings,Frodo Baggins2,Harry Potter,Harry Potter

In this example, we have created the CSV file named protagonist.csv in the writing mode.

We then used the csv.writer() function to write to the file. To learn more about writing to a csv file, Python Writing CSV Files.

Here,

  • writer.writerow(["SN", "Movie", "Protagonist"]) writes the header row with column names to the CSV file.
  • writer.writerow([1, "Lord of the Rings", "Frodo Baggins"]) writes the first data row to the CSV file.
  • writer.writerow([2, "Harry Potter", "Harry Potter"]) writes the second data row to the CSV file.

Writing Dictionaries to CSV Files

We can use the csv.DictWriter() class to write dictionary data into a CSV file, which is useful for more structured data. For example,

import csvwith open('players.csv', 'w', newline='') as file: fieldnames = ['player_name', 'fide_rating'] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() writer.writerow({'player_name': 'Magnus Carlsen', 'fide_rating': 2870}) writer.writerow({'player_name': 'Fabiano Caruana', 'fide_rating': 2822}) writer.writerow({'player_name': 'Ding Liren', 'fide_rating': 2801})

The program creates a players.csv file with the following entries:

player_name,fide_ratingMagnus Carlsen,2870Fabiano Caruana,2822Ding Liren,2801

In this example, we have written data to the players.csv file using csv.DictWriter(file, fieldnames=fieldnames).

The writer.writeheader() function writes these column headers to the first row of the file.

And each call to writerow() adds a new row to the CSV file, where each dictionary represents a record with player_name and fide_rating as keys corresponding to the columns.

Using Python Pandas to Handle CSV Files

Pandas is a popular data science library in Python for data manipulation and analysis.

If we are working with huge chunks of data, it's better to use pandas to handle CSV files for ease and efficiency.

Note: Before we can use pandas, we need to install and import it. To learn more, visit Install and Import Pandas.

Read CSV Files

To read the CSV file using pandas, we can use the read_csv() function.

import pandas as pdpd.read_csv("people.csv")

Here, the program reads people.csv from the current directory.

Write to a CSV Files

To write to a CSV file, we need to use the to_csv() function of a DataFrame.

import pandas as pd# creating a data framedf = pd.DataFrame([['Jack', 24], ['Rose', 22]], columns = ['Name', 'Age'])# writing data frame to a CSV filedf.to_csv('person.csv')

Here, we have created a DataFrame using the pd.DataFrame() method. Then, the to_csv() function for this object is called, to write into person.csv.

Also Read

  • Python File Operation
Read and Write CSV files (2024)
Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6102

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.