Working with CSV files is a common task in data processing and analysis. CSV stands for Comma Separated Values and represents a format for storing tabular data in a plain text file with each value separated by commas.
In Python, we can use the built-in csv module to read and write CSV files easily. In this tutorial, we’ll learn how to open and read a CSV file, parse its content, and work with the parsed data.
Step 1: Prepare a CSV File
Before we start, let’s create an example CSV file “sample.csv” to work with. The file should have the following content:
Name,Age,Location Alice,30,New York Bob,25,Los Angeles Charlie,22,Chicago Diana,27,San Francisco Eva,24,Seattle
This file contains five rows with a header describing the columns: Name, Age, and Location. Each row provides information about a person.
Step 2: Import the CSV Module
To work with CSV files in Python, you need to import the csv module. Include the following line at the beginning of your Python script:
1 |
import csv |
This will make the csv module available in your script, allowing you to read and write CSV files.
Step 3: Open and Read the CSV File
Use the with open()
statement to open the file, providing the file name and the mode as ‘r’ for reading. After opening the file, we can use the csv.reader
function to read the content. Here’s an example of how to open and read the sample CSV file:
1 2 3 4 5 6 7 8 |
import csv # Open and read the CSV file with open('sample.csv', mode='r') as csvfile: csv_reader = csv.reader(csvfile, delimiter=',') for row in csv_reader: print(row) |
In this example, we open the ‘sample.csv’ file and use the csv.reader
function to parse the file. The delimiter
parameter is set to ‘,’ because the values in our CSV file are separated by commas. The code then prints each row of the CSV file.
Step 4: Work with the Parsed Data
Now that you can open and read a CSV file, you can start working with the parsed data. In this example, let’s print each person’s name and location:
1 2 3 4 5 6 7 8 9 10 |
import csv with open('sample.csv', mode='r') as csvfile: csv_reader = csv.reader(csvfile, delimiter=',') # Skip the header row next(csv_reader) for row in csv_reader: print(f'{row[0]} lives in {row[2]}') |
To skip the header row, we use the next()
function before iterating through the rows. This allows us to only print the data rows.
Full Code
1 2 3 4 5 6 7 8 9 10 |
import csv with open('sample.csv', mode='r') as csvfile: csv_reader = csv.reader(csvfile, delimiter=',') # Skip the header row next(csv_reader) for row in csv_reader: print(f'{row[0]} lives in {row[2]}') |
Output
Alice lives in New York Bob lives in Los Angeles Charlie lives in Chicago Diana lives in San Francisco Eva lives in Seattle
Conclusion
In this tutorial, we learned how to open and read a CSV file in Python using the built-in csv module. We covered the steps to import the csv module, open a CSV file, read its content, and work with the parsed data. This basic knowledge will help you when working with other modules or libraries that require reading or writing CSV files, such as Pandas or NumPy.