Append Data to an Existing CSV File using Python

To append data to an existing CSV file in Python, we use the built-in csv module with the open() function in append mode ('a'). This allows us to add new rows to the file without overwriting the existing content. In this tutorial, we will explore different methods for appending data to a CSV file.


Examples to Append Data to a CSV File

1. Appending a Single Row to a CSV File

In this example, we will append a single row of data to an existing CSV file named students.csv.

We will use the csv.writer() method and the append mode ('a').

main.py

</>
Copy
import csv

# Open the CSV file in append mode ('a')
with open("students.csv", mode="a", newline="") as file:
    writer = csv.writer(file)
    
    # Appending a single row
    writer.writerow(["Johny", 20, "Computer Science"])

print("Data appended successfully!")

Explanation:

  1. import csv: Imports the csv module to handle CSV operations.
  2. open("students.csv", mode="a", newline=""): Opens the file in append mode ('a') so that new rows are added without deleting existing content.
  3. csv.writer(file): Creates a CSV writer object.
  4. writer.writerow(["John Doe", 20, "Computer Science"]): Appends a new row with student data.
  5. print("Data appended successfully!"): Confirms successful execution.

Output:

Data appended successfully!

2. Appending Multiple Rows to a CSV File

In this example, we will append multiple rows of data to an existing CSV file using the writerows() method.

main.py

</>
Copy
import csv

# Data to append
new_students = [
    ["Priya", 22, "Mathematics"],
    ["Satya", 19, "Physics"],
    ["Sundar", 21, "Biology"]
]

# Open the CSV file in append mode
with open("students.csv", mode="a", newline="") as file:
    writer = csv.writer(file)
    
    # Appending multiple rows
    writer.writerows(new_students)

print("Multiple rows appended successfully!")

Explanation:

  1. new_students: A list of lists containing new student records.
  2. open("students.csv", mode="a", newline=""): Opens the file in append mode. Reference: open() builtin function
  3. csv.writer(file): Creates a writer object to handle CSV operations.
  4. writer.writerows(new_students): Writes multiple rows from the new_students list.
  5. print("Multiple rows appended successfully!"): Displays confirmation message.

Output:

Multiple rows appended successfully!

3. Appending Data Using a Dictionary

In this example, we will append data using a dictionary, where keys represent column names.

main.py

</>
Copy
import csv

# Data to append
new_student = {"Name": "Lee", "Age": 23, "Subject": "Chemistry"}

# Open the CSV file in append mode
with open("students.csv", mode="a", newline="") as file:
    fieldnames = ["Name", "Age", "Subject"]
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    
    # Appending dictionary row
    writer.writerow(new_student)

print("Dictionary data appended successfully!")

Explanation:

  1. new_student: A dictionary where keys represent column names.
  2. fieldnames = ["Name", "Age", "Major"]: Defines the column headers.
  3. csv.DictWriter(file, fieldnames=fieldnames): Creates a dictionary-based writer object.
  4. writer.writerow(new_student): Writes the dictionary data as a new row.

Output:

Dictionary data appended successfully!

Conclusion

In this tutorial, we covered different ways to append data to an existing CSV file:

  1. Using writerow(): Adds a single row to the file.
  2. Using writerows(): Appends multiple rows at once.
  3. Using DictWriter: Appends data using a dictionary format.

Appending data to a CSV file is useful for logging data, updating records, and handling dynamic datasets.