If you need to read CSV in Python like reading CSV files (and writing) to them, you’re in luck. In this tutorial, you’re going to learn how to read, write to and append data to CSV files in your Python scripts!
Let’s get started!
Prerequisites
This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:
- A Windows or Linux host with Python 3 installed. This tutorial will use Windows, but Linux will work fine also.
- A code editor like VS Code to copy and paste Python code snippets to.
How to Read CSV in Python
Let’s get started and see how you can read CSV in Python. Python provides a built-in module called csv
that has various methods allowing you to work with CSV files so let’s use that.
To read CSV files, the Python csv
module provides a method called reader()
. Let’s first demonstrate how to use this method.
1. Create a directory at ~/pythoncsvdemo and download this csv file into it. The example CSV contains a list of fictitious people with columns of “Name,” “Sex,” “Age,” “Height (in),” and “Weight (lbs).” This CSV file will be used throughout this tutorial.
2. Next, open a code editor, paste the following Python script into it. This simple script imports the csv
module and uses the reader()
method to read the file and iterate over each line in the CSV file with a for
loop.
import csv #import to use the csv module
with open('demo_csv.csv', mode="r") as csv_file: #"r" represents the read mode
reader = csv.reader(csv_file) #this is the reader object
for item in reader:
# you have to loop through the document to get each data
print(item)
Replace the
reader()
method with thedictreader()
method to return CSV rows in a Python dictionary rather than an array.
In the below output, you’ll see the first line is the name of the columns, with each row representing a CSV row. Each column represents an index starting from 0.
3. Perhaps you’d prefer only to see the output of one column. No problem. Provide the index number of 1
against the item
variable representing the row.
import csv
with open('demo_csv.csv', mode="r") as csv_file:
reader = csv.reader(csv_file)
for item in reader:
print(item[1])# index is added to get a particular column
If you’re using the
dictreader()
method, replace theprint()
command above withprint(item["Name"])
. Sincedictreader()
creates a dictionary for each CSV row, you can reference columns in the row by name instead of index number.
Creating CSV Files with Python
Using the csv
module and a few handy methods, you can also write to CSV files from Python. To write a CSV file, create another script with the following code.
This script defines each column that will be in the CSV (column_name
) along with each element in a single row (data
). The script then opens the demo_csv1.csv
for writing (w
) and writes a single row (writerow()
).
import csv
column_name = ["Name", "Sex", "Age", "Height (in)", "Weight (lbs)"] #The name of the columns
data = ['Ali',"M", 29, 71,176] #the data
with open('demo_csv1.csv', 'w') as f:
writer = csv.writer(f) #this is the writer object
writer.writerow(column_name) # this will list out the names of the columns which are always the first entrries
writer.writerow(data) #this is the data
If you need to append row(s) to a CSV file, replace the write mode (
w
) with append mode (a
) and skip writing the column names as a row (writer.writerow(column_name)
).
You’ll see below that Python creates a new CSV file (demo_csv1.csv), with the first row contains the column names and the second row includes the first row of data.
Appending to a CSV file with a Dictionary
If you’d prefer to use a dictionary, change your script slightly to use the dictwriter()
method providing each field or column name as an argument (fieldnames=field_names
), as shown below.
import csv
# list of column names
field_names = ['Name','Sex','Age','Height (in)','Weight (lbs)']
# Dictionary
dict = {"Name": "Muhammed", "Sex":"F","Age":10, "Height (in)":34, "Weight (lbs)": 139}
with open('demo_csv.csv', 'a') as csv_file:
dict_object = csv.DictWriter(csv_file, fieldnames=field_names)
dict_object.writerow(dict)
Reading from One CSV File to Write To Another
Perhaps you already have an existing CSV file and would like to use it as input to another CSV file. You can make it happen by using a combination of read mode and the reader()
method and write mode and the writer()
method.
The following script:
- Opens an existing file called
demo_csv.csv
in read mode - Reads the file as a CSV with the
reader()
method - Opens another CSV called
new_demo_csv.csv
in write mode - Reads each row in the source CSV file and writes those roles in the destination CSV file using the
-
delimiter.
import csv
with open("demo_csv.csv", mode="r") as old_file:
reader_obj = csv.reader(old_file) #read the current csv file
with open("new_demo_csv.csv", mode="w") as new_file:
writer_obj = csv.writer(new_file, delimiter="-") # Writes to the new CSV file
for data in reader_obj:
#loop through the read data and write each row in new_demo_csv.csv
writer_obj.writerow(data)
Deleting Columns from CSV Files with Python
Let’s wrap up this tutorial by removing columns from CSV files. Unfortunately, eliminating columns isn’t as straightforward as reading or writing to CSV files, but you’ll see it still definitely possible.
To remove fields from a CSV file, you can’t directly remove them. Instead, you must read all of the fields in the CSV file and then write to another CSV file, excluding all fields you don’t want, like below.
import csv
with open("demo_csv.csv", mode="r") as original:
reader_obj = csv.reader(original)
with open("output.csv", mode="w") as new:
writer_obj = csv.writer(new)
for column in reader_obj:
writer_obj.writerow((column[0], column[1], column[2])) # this represents the columns you need
Conclusion
You should now have some foundational knowledge to read CSV in Python, write to CSV files, and even remove fields from CSV files. Using the Python csv
module with its various methods, you can make quick work of CSV files in Python!
How do you plan to incorporate this newly discovered knowledge into your Python projects?