Opening CSV files in Python

Opening CSV Files with Python

Comma-Separated Values (CSV) files are a common file format, often used to store tabular data. Python provides many libraries for reading and writing CSV files. This article explains how to use Python to open and read CSV files.

CSV File Format

A CSV file is a text file consisting of multiple lines of data, each separated by a comma, tab, or other delimiter. The first line of a file typically contains column headers, followed by the data for each column.

Here’s an example of a simple CSV file with three columns:

Name, Age, Gender
Tom, 25, Male
Lucy, 24, Female

Reading CSV Files

Python’s csv module provides a simple way to read and process CSV files. First, we need to import the csv module:

import csv

Reading an Entire CSV File

To read an entire CSV file, we can use the csv.reader object.

import csv

with open('filename.csv', newline='') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
print(row)

The above code opens a file named filename.csv and reads all rows from it. The csvreader object converts each row of data into a list with comma-separated values.

Note that we use the newline=”” parameter to avoid double line breaks.

Reading a Specific Line

To read a specific line, we can use the next() function to move the csvreader object from the beginning of the file to the specified line.

import csv

with open('filename.csv', newline='') as csvfile:
csvreader = csv.reader(csvfile)
# Move to the second line
next(csvreader)
# Read the third line
row = next(csvreader)
print(row)

The above code demonstrates how to read the third line of data from the file (i.e., Lucy, 24, Female).

Specifying Delimiters and Quote Characters

Some CSV The file uses a non-comma delimiter or quotes to represent the delimiters in the data. In this case, we need to specify the delimiter and quote parameters.

import csv

with open('filename.csv', newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';', quotechar='"')
for row in csvreader:
print(row)

The above code opens a file named filename.csv and specifies a semicolon as the delimiter and double quotes as the quotes.

Writing CSV Files

Similar to reading CSV files, Python’s csv module also provides a simple method for writing CSV files.

import csv

rows = [
['Tom', 25, 'Male'],
['Lucy', 24, 'Female'],
['John', 30, 'Male']
]

with open('filename.csv', 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(['Name', 'Age', 'Gender'])
for row in rows:
csvwriter.writerow(row)

The above code opens a file named filename.csv and writes data to it. We first use the csvwriter.writerow() method to write the first row of data (the header row) and then use a for loop to write each row of data.

In addition to the csv.writer() method, the csv module also provides the csv.DictWriter() method for writing dictionary data to a CSV file.

import csv

rows = [
    {'Name': 'Tom', 'Age': 25, 'Gender': 'Male'},
    {'Name': 'Lucy', 'Age': 24, 'Gender': 'Female'},
    {'Name': 'John', 'Age': 30, 'Gender': 'Male'}
]

with open('filename.csv', 'w', newline='') as csvfile:
    fieldnames = ['Name', 'Age', 'Gender']
    csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
    csvwriter.writeheader()
    forrow in rows: csvwriter.writerow(row)

The code above is similar to the previous example, except that we use the csv.DictWriter() method and store the data in a list of dictionaries.

Summary

Python’s csv module provides a convenient way to read and write CSV files. Using the csv.reader and csv.writer objects, we can easily read and write data from CSV files.

To read a CSV file, we use the csv.reader object and iterate over the rows. By setting the correct delimiter and quote characters, we can handle files with non-comma delimiters.

To write a CSV file, we can use either the csv.writer or csv.DictWriter objects. The csv.DictWriter object also allows us to write dictionary data to a CSV file.

For more details on the csv module, see the official documentation: https://docs.python.org/3/library/csv.html.

Leave a Reply

Your email address will not be published. Required fields are marked *