The CSV module provides functionalities to read from and write data to comma-separated values (CSV) files in Python. CSV files are popular for storing structured data because they are simple, accessible, and easily manipulated with basic spreadsheet programs. In this tutorial, we will learn how to install the CSV module in Python and use it to handle various CSV-related tasks.
Step 1: Install Python
Before installing the CSV module, make sure you have the latest version of Python installed on your computer. If you do not have Python installed, visit the official Python download page to download and install the appropriate version for your operating system.
Step 2: Check if CSV Module is Already Installed
By default, the CSV module is bundled with Python. To check if the CSV module is installed, open a Python terminal (or the IDLE shell) and run the following command:
1 |
import csv |
If no error message appears, the CSV module is already installed, and you can skip the next step. Otherwise, proceed to step 3.
Step 3: Install CSV Module Using pip
If the CSV module is not installed, you can use the Python package manager, pip, to install it. Open a command prompt (Windows) or terminal (MacOS/Linux) and run the following command:
1 |
pip install python-csv |
This will install the CSV module in your Python environment, and you should now be able to import it successfully.
Step 4: Read CSV File with CSV Module in Python
With the CSV module installed, you can now use Python to efficiently read and process CSV data. Use the following code to read data from a CSV file named “example_file.csv”:
1 2 3 4 5 6 7 8 9 |
import csv filename = 'example_file.csv' with open(filename, 'r') as csvfile: csv_reader = csv.reader(csvfile) for row in csv_reader: print(row) |
This code reads the contents of “example_file.csv” row by row and prints each row as a list.
Output:
['row1_col1', 'row1_col2', 'row1_col3'] ['row2_col1', 'row2_col2', 'row2_col3'] ['row3_col1', 'row3_col2', 'row3_col3']
Replace “example_file.csv” with the name of a CSV file on your computer to see the code in action.
Step 5: Write to a CSV File Using Python and CSV Module
Similarly, you can use the CSV module to write data to a new CSV file. The following code creates a new CSV file called “example_output_file.csv” and writes sample data to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import csv filename = 'example_output_file.csv' data = [ ['header1', 'header2', 'header3'], ['row1_col1', 'row1_col2', 'row1_col3'], ['row2_col1', 'row2_col2', 'row2_col3'] ] with open(filename, 'w', newline='') as csvfile: csv_writer = csv.writer(csvfile) for row in data: csv_writer.writerow(row) |
Conclusion
In this tutorial, you learned how to install the CSV module in Python and use it to read and write CSV files. With the CSV module, handling structured data in Python is simple, efficient, and versatile. Happy coding!
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import csv # Read from CSV File filename = 'example_file.csv' with open(filename, 'r') as csvfile: csv_reader = csv.reader(csvfile) for row in csv_reader: print(row) # Write to CSV File filename_output = 'example_output_file.csv' data = [ ['header1', 'header2', 'header3'], ['row1_col1', 'row1_col2', 'row1_col3'], ['row2_col1', 'row2_col2', 'row2_col3'] ] with open(filename_output, 'w', newline='') as csvfile: csv_writer = csv.writer(csvfile) for row in data: csv_writer.writerow(row) |