top of page

Reading and Writing CSV Files in Python

CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. It is one of the most common methods for exchanging data between applications and popular data format used in Data Science. It is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separated by a delimiter(comma in most cases). To represent a CSV file, it must be saved with the .csv file extension.

Reading from CSV file

Python contains a module called csv for the handling of CSV files. The reader class from the module is used for reading data from a CSV file. At first, the CSV file is opened using the open() method in ‘r’ mode(specifies read mode while opening a file) which returns the file object then it is read by using the reader() method of CSV module that returns the reader object that iterates throughout the lines in the specified CSV document.


Python provides a built-in module called csv that makes it easy to handle CSV files. Here's an example of how you can read and write CSV files in Python:

Reading a CSV File: To read a CSV file, you can use the csv.reader class. Here's an example:

import csv

# Open the CSV filewith open('data.csv', 'r') as file:
    # Create a CSV reader object
    csv_reader = csv.reader(file)

    # Read each row of the CSV filefor row in csv_reader:
        # Access the data in each column of the row
        column1 = row[0]
        column2 = row[1]
        # Process the data as needed# ...

In this example, data.csv is the name of the CSV file. We open the file using the open() function and pass the file object to the csv.reader class to create a reader object. We can then iterate over the reader object to access each row of the CSV file. The data in each row can be accessed using indexing, with row[0] representing the first column, row[1] representing the second column, and so on.

Writing to a CSV File: To write data to a CSV file, you can use the csv.writer class. Here's an example:

import csv

# Data to be written to the CSV file
data = [
    ['Name', 'Age', 'Country'],
    ['John', '25', 'USA'],
    ['Emily', '30', 'Canada'],
    ['Sam', '22', 'Australia']
]

# Open the CSV file in write modewith open('output.csv', 'w', newline='') as file:
    # Create a CSV writer object
    csv_writer = csv.writer(file)

    # Write the data to the CSV file
    csv_writer.writerows(data)

In this example, we define a list called data that contains the rows to be written to the CSV file. We open the file using the open() function with the newline='' argument to prevent extra blank lines in the output. We then pass the file object to the csv.writer class to create a writer object. Finally, we use the writerows() method to write the data to the CSV file.

Remember to replace 'data.csv' and 'output.csv' with the actual file names you want to read from and write to, respectively.

Related Posts

See All

Mastering Lists in Python

Introduction Lists are one of the most fundamental and versatile data structures in Python. They allow you to store and manipulate...

Python Anonymous Function

In Python, an anonymous function is a function that is defined without a name. It is also known as a lambda function because it is...

Comments


bottom of page