Python Add Rows To CSV File (Append Prepend Insert)

Welcome to a tutorial on how to append, prepend, and insert new rows into a CSV file in Python. So you want to add new rows to a CSV file? It can be done with just 3 lines of code.

  • import csv
  • writer = csv.writer(open("FILE.CSV", "a", newline=""))
  • writer.writerow(["A", "B"])

This will append a new row to the bottom of the CSV file. But how about prepending and inserting at an exact location? Read on for more examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PYTHON ADD NEW ROWS TO CSV

All right, let us now get into the examples of adding new rows into an existing CSV file – Append, prepend, and insert.

 

1) APPEND ROWS TO CSV FILE

1-append.py
# (A) LOAD CSV MODULE
import csv

# (B) OPEN CSV & APPEND ROWS
with open("demo.csv", "a", newline="") as csvfile:
  writer = csv.writer(csvfile)
  writer.writerow(["A", "B"])
  writer.writerows([["C", "D"], ["E", "F"]])

As in the introduction snippet above:

  • import csv This is a built-in module, and there is no need to pip install.
  • with open("demo.csv", "a", newline="") as csvfile Open the CSV file in append mode. Take note that we override new lines with an empty string. This is because of a “bug” where writerow() will append a line break, and we don’t want open() to insert another one.
  • writer = csv.writer(csvfile) Create a CSV writer.
  • Lastly, two ways to append CSV rows –
    • writerow() Append a single row.
    • writerows() Append multiple rows.

 

2) PREPEND ROWS TO CSV FILE

2-prepend.py
# (A) LOAD CSV MODULE
import csv

# (B) READ EXISTING ROWS INTO A LIST
with open("demo.csv", "r", newline="") as csvfile:
  rows = list(csv.reader(csvfile))

# (C) PREPEND NEW ROWS
rows = [["G", "H"], ["I", "J"]] + rows

# (D) SAVE UPDATED CSV
with open("demo.csv", "w", newline="") as csvfile:
  writer = csv.writer(csvfile)
  writer.writerows(rows)

writerow() will only append to the end of the CSV file. So if you want to prepend rows at the top, we need a little bit of “data yoga”:

  • (A) Load the CSV module as usual.
  • (B & C) Read the CSV file into a list – Prepend your new rows by concatenating them with the existing rows.
  • (D) Finally, save the updated rows back into the CSV file.

 

 

3) INSERT ROWS INTO CSV FILE

3-insert.py
# (A) LOAD CSV MODULE
import csv

# (B) READ EXISTING ROWS INTO A LIST
with open("demo.csv", "r", newline="") as csvfile:
  rows = list(csv.reader(csvfile))

# (C) INSERT ROWS
rows.insert(3, ["K", "L"])

# (D) SAVE UPDATED CSV
with open("demo.csv", "w", newline="") as csvfile:
  writer = csv.writer(csvfile)
  writer.writerows(rows)

If you need to “insert at exactly row X”:

  • (A & B) The same “read CSV file into a list”.
  • (C) A friendly reminder of the very useful list function called insert().
  • (D) Save the updated rows into the CSV file.

 

4) SEARCH & INSERT INTO CSV FILE

4-search.py
# (A) LOAD CSV MODULE
import csv

# (B) READ EXISTING ROWS INTO A LIST
with open("demo.csv", "r", newline="") as csvfile:
  rows = list(csv.reader(csvfile))

# (C) INSERT BEFORE "JON DOE"
at = 0
for i, r in enumerate(rows):
  if "Jon Doe" in r:
    at = i
    break

# (D) INSERT NEW ROWS
rows.insert(at, ["M", "N"])

# (E) SAVE UPDATED CSV
with open("demo.csv", "w", newline="") as csvfile:
  writer = csv.writer(csvfile)
  writer.writerows(rows)

The above example is great if you know exactly where to insert the new row. But if you don’t… The only way is to loop through the entire list to determine which row to insert into.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!