How do i read and append a csv file in python?

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
  • How to Read CSV in Python
  • Creating CSV Files with Python
  • Appending to a CSV file with a Dictionary
  • Reading from One CSV File to Write To Another
  • Deleting Columns from CSV Files with Python
  • Conclusion

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 the dictreader() 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.

How do i read and append a csv file in python?
CSV output via the reader() method

How do i read and append a csv file in python?
CSV output via the dicteader() method

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 the print() command above with print(item["Name"]). Since dictreader() creates a dictionary for each CSV row, you can reference columns in the row by name instead of index number.

How do i read and append a csv file in python?
Display list of Sexes

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.

How do i read and append a csv file in python?
New CSV file created with Python

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)

How do i read and append a csv file in python?
Appending to a CSV file with a Dictionary

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?

How do I add data to an existing csv file in python?

Append a dictionary as a row to an existing csv file using DictWriter in python.
import csv module's DictWriter class,.
Open our csv file in append mode and create a file object,.
Pass the file object & a list of csv column names to the csv. ... .
This DictWriter object has a function writerow() that accepts a dictionary..

How do I open a CSV file and append it?

There are several steps to do that..
Import DictWriter class from CSV module..
Open your CSV file in append mode. Create a file object for this file..
Pass the file object and a list of column names to DictWriter() ... .
Pass the dictionary as an argument to the Writerow() function of DictWriter. ... .
Close the file object..

How do I read a csv file in python?

Read A CSV File Using Python.
Using the CSV Library. import csv with open("./bwq.csv", 'r') as file: csvreader = csv.reader(file) for row in csvreader: print(row) Here we are importing the csv library in order to use the . ... .
Using the Pandas Library. import pandas as pd data = pd.read_csv("bwq.csv") data..

How do you read from one csv file and write to another in python?

The csv module in Python's standard library presents classes and methods to perform read/write operations on CSV files..
writer() ... .
writerow() ... .
writerows() ... .
read() ... .
DictWriter() ... .
writeheader() ... .
DictReader().