Table of Contents
Introduction
CSV (Comma-Separated Values) files are a common way to store and exchange tabular data. They are widely used in various industries and can be easily manipulated using Python.
In this tutorial, we will learn how to work with CSV files in Python. By the end of this tutorial, you will be able to read and write CSV files, extract data from them, and perform basic data manipulations.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of Python programming language and have Python installed on your system. If you need help installing Python, you can refer to the official Python documentation.
Setup
Before we begin, let’s make sure we have the necessary modules installed. Python provides a built-in module called csv
that allows us to easily work with CSV files. There is no additional installation required.
Now that we have everything set up, let’s dive into reading and writing CSV files using Python.
Reading CSV Files
To read a CSV file in Python, we can use the csv.reader()
function from the csv
module. The csv.reader()
function takes a file object as the argument and returns an object that we can iterate over to access the rows in the CSV file.
Here’s an example: ```python import csv
# Open the CSV file
with open('data.csv', 'r') as file:
# Create a CSV reader object
csv_reader = csv.reader(file)
# Iterate over each row in the CSV file
for row in csv_reader:
# Access each column of the row
column1 = row[0]
column2 = row[1]
column3 = row[2]
# Do something with the data
print(column1, column2, column3)
``` In the above code, we first open the CSV file using the `open()` function and a file mode of `'r'` (which means read mode). We then pass the file object to the `csv.reader()` function to create a CSV reader object.
Next, we iterate over each row in the CSV file using a for
loop. Inside the loop, we can access each column of the row by indexing the row
object with the column index. In this example, we assume the CSV file has three columns.
Finally, we can perform any operations or manipulations on the data, in this case, we simply print the values of each column.
Writing CSV Files
Writing data to a CSV file follows a similar process. We use the csv.writer()
function to create a CSV writer object, and then we can use the writerow()
method to write each row of data to the file.
Here’s an example: ```python import csv
# Data to be written to the CSV file
data = [
['Name', 'Age', 'Email'],
['John Doe', 28, '[email protected]'],
['Jane Smith', 32, '[email protected]'],
['Bob Johnson', 45, '[email protected]']
]
# Open the CSV file
with open('data.csv', 'w', newline='') as file:
# Create a CSV writer object
csv_writer = csv.writer(file)
# Write the data to the CSV file
for row in data:
csv_writer.writerow(row)
``` In this example, we define the data to be written in a nested list. Each inner list represents a row in the CSV file.
Similar to reading a CSV file, we open the file using the open()
function with a file mode of 'w'
(which means write mode). We pass the file object to the csv.writer()
function to create a CSV writer object.
Next, we iterate over each row of data using a for
loop and call the writerow()
method of the CSV writer object to write each row to the file.
Conclusion
In this tutorial, we learned how to work with CSV files in Python. We saw how to read and write CSV files using the csv
module, iterate over rows and columns, and perform basic data manipulations.
CSV files are a popular and versatile way to store and exchange data. Being able to work with CSV files in Python opens up a wide range of possibilities for data processing and analysis.
We covered the basics of working with CSV files, but there is much more you can do. I encourage you to explore the csv
module documentation to discover additional functionalities and options.
I hope this tutorial has been informative and helps you in your Python journey!