Working with CSV files is a very common task when it comes to data manipulation and analysis. Often times, you may need to work with more than one CSV file and combine the data in those files for further processing. In this tutorial, we will learn how to open and read two CSV files in Python using the built-in csv
module.
Step 1: Install Python and a Text Editor
Before we begin, make sure you have Python installed on your machine. If not, you can download it from the official Python website. Additionally, you’ll also need a text editor to write your code. Some recommended text editors are Visual Studio Code, Sublime Text, or Atom.
Step 2: Create Two CSV Files
First, let’s create two sample CSV files, file1.csv
and file2.csv
. Create these files in the same directory as your Python script.
For file1.csv
, use the following content:
name,age,city Alice,30,New York Bob,25,Los Angeles Charlie,22,San Francisco
For file2.csv
, use the following content:
name,age,city David,28,Boston Eva,24,Chicago Fiona,31,Seattle
Step 3: Opening the CSV Files in Python
To open the CSV files in Python, create a new Python script and import the csv
module as shown below:
1 |
import csv |
Next, open both files in read mode using the open()
function and the with
statement. The with
statement allows you to handle the files without worrying about closing them after usage.
1 2 3 4 |
import csv with open('file1.csv', mode='r') as f1, open('file2.csv', mode='r') as f2: # Code to read and process the CSV files comes here |
Step 4: Reading the CSV Files Using the csv.reader
Object
Now that we have both files opened, let’s read their content using the csv.reader
object. By looping through the csv.reader
object, we can access all the rows in each CSV file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import csv with open('file1.csv', mode='r') as f1, open('file2.csv', mode='r') as f2: csv_reader1 = csv.reader(f1) csv_reader2 = csv.reader(f2) # Loop through the rows of file1.csv for row in csv_reader1: print(row) # Loop through the rows of file2.csv for row in csv_reader2: print(row) |
This code will output the following:
['name', 'age', 'city'] ['Alice', '30', 'New York'] ['Bob', '25', 'Los Angeles'] ['Charlie', '22', 'San Francisco'] ['name', 'age', 'city'] ['David', '28', 'Boston'] ['Eva', '24', 'Chicago'] ['Fiona', '31', 'Seattle']
Now you know how to open and read two CSV files using Python’s built-in csv
module. You can further modify the code to combine or process the data as per your requirements.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import csv with open('file1.csv', mode='r') as f1, open('file2.csv', mode='r') as f2: csv_reader1 = csv.reader(f1) csv_reader2 = csv.reader(f2) # Loop through the rows of file1.csv for row in csv_reader1: print(row) # Loop through the rows of file2.csv for row in csv_reader2: print(row) |
Conclusion
In this tutorial, we learned how to open and read two CSV files in Python using the built-in csv
module. By following these steps, you can easily work with multiple CSV files, manipulate their data, and use it for further data analysis or other tasks. Remember to always close your files or use the with
statement to ensure proper file handling.