Skip to content

How to append data to a csv file in Python (in 3 ways)

How to append data to a csv file in Python

In this tutorial we will learn how to append data to a csv file in Python . Before reading this tutorial we recommended to read about the below tutorials –

Below is the two csv files named demo1.csv and demo2.csv . We will save both the files in the current working folder where we are working now –

There are multiple ways to append data to a csv file in Python . Here we will see 3 ways to append data to a csv file in Python-

Append data to a csv file in Python using CSV writer() method

import csv

with open("demo2.csv",newline="") as demo2:
    # creating reader object
    csv_reader=csv.reader(demo2)
    # reading the file
    data=[]
    for row in csv_reader:
        data.append(row)

with open("demo1.csv","a",newline="") as demo1:
    # creating writer object
    csv_writer=csv.writer(demo1)
    # appending data
    csv_writer.writerows(data[1:])     
  • In the above code we have appended data of demo2.csv file to the demo1.csv file . For this reason first we have imported csv module .
  • After that we have opened the demo2.csv file in “r” mode . Then using csv.reader() method we have stored all the data of the file to the csv_reader variable . After that we have created an empty list named data and append all the data to the list by iterating the csv_reader using for loop .
  • Then we have opened the demo1.csv file in “a” mode . After that we have created a csv-writer object csv_writer using csv.writer() method . Then we have appended all the data to the demo1.csv using writerrows() method . To avoid heading from the demo2.csv file we have used data[1:] .

Append data to a csv file in Python using CSV DictWriter() method

import csv

with open("demo2.csv",newline="") as demo2:
    # creating reader object
    csv_reader=csv.DictReader(demo2)
    # reading the file
    data=[]
    for row in csv_reader:
        data.append(row)

with open("demo1.csv","a",newline="") as demo1:
    # creating writer object
    csv_writer=csv.DictWriter(demo1,fieldnames=data[0].keys())
    # appending data
    csv_writer.writerows(data) 
  • In the above code we have appended data of demo2.csv file to the demo1.csv file . For this reason first we have imported csv module .
  • After that we have opened the demo2.csv file in “r” mode . Then using csv.DictReader() method we have stored all the data of the file to the csv_reader variable . After that we have created an empty list named data and append all the data to the list by iterating the csv_reader using for loop . So every item in the data list is a dictionary .
  • Then we have opened the demo1.csv file in “a” mode . After that we have created a csv-writer object csv_writer using csv.DictWriter() method . Then we have appended all the data to the demo1.csv using writerrows() method . As every item in the data list is dictionary so we have used fieldnames parameter as data[0].keys() in the csv.DictWriter() method .

Append data to a csv file in Python using pandas

# using pandas
import pandas as pd

# reading data from demo2.csv
data=pd.read_csv("demo2.csv")

# appending data to demo1.csv
data.to_csv("demo1.csv",index=False,header=False,mode="a")
  • In the above code we have appended data of demo2.csv file to the demo1.csv file . For this reason first we have imported pandas module .
  • After that we have read the demo2.csv file using pd.read_csv() method and stored the data in the data variable .
  • Then we have appended the data to the demo1.csv using to_csv() method . In the to_csv() method we have to use mode parameter value as “a” . To avoid the header of demo2.csv file we have used header parameter value as False .

Conclusion

In this tutorial we have learnt three methods to append data to a csv file in Python . Now it is on you and your requirement which method is appropriate for your use-cases .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

3 thoughts on “How to append data to a csv file in Python (in 3 ways)”

Leave a Reply