Page Contents
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 –
- CSV file handling in Python – An Intro for Beginners
- File Handling in Python – An Intro for Beginners
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 thecsv_reader
variable . After that we have created an empty list nameddata
and append all the data to the list by iterating thecsv_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
usingcsv.writer()
method . Then we have appended all the data to the demo1.csv usingwriterrows()
method . To avoid heading from the demo2.csv file we have useddata[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 thecsv_reader
variable . After that we have created an empty list nameddata
and append all the data to the list by iterating thecsv_reader
using for loop . So every item in thedata
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
usingcsv.DictWriter()
method . Then we have appended all the data to the demo1.csv usingwriterrows()
method . As every item in thedata
list is dictionary so we have usedfieldnames
parameter asdata[0].keys()
in thecsv.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 thedata
variable . - Then we have appended the data to the demo1.csv using
to_csv()
method . In theto_csv()
method we have to usemode
parameter value as “a” . To avoid the header of demo2.csv file we have usedheader
parameter value asFalse
.
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 .
I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
Hello, I enjoy reading through your article post. I like to write a littloe
comment to support you.
Take a look at my webpage – https://Casinobitstarz.Webgarden.com
Hey There. I found your blog the use of msn. This is a really smartly written article. I’ll be sure to bookmark it and return to learn extra of your helpful info. Thanks for the post. I’ll certainly return.