Lists are one of the most frequently used data structures in python. In this article, we will discuss how we can append a list to a CSV file in python.
Append List to CSV File in Python Using csv.writer()
The csv module provides us with different methods to perform various operations on a CSV file. To append a list to csv file in python, we can use the csv.writer()
method along with the csv.writerow()
method. For this, we will use the following steps.
- First, we will open a csv file in append mode using the
open()
function. Theopen()
function takes the file name as the first input argument and the literal “a” as the second input argument to show that the file will be opened in the append mode. It returns a file object that contains the csv file opened by theopen()
function. - After opening the file, we will create a
csv.writer
object using thecsv.writer()
method. Thecsv.writer()
method takes the file object as an input argument and returns a writer object. Once the writer object is created, we can append the list to the csv file using thecsv.writerow()
method. - The
csv.writerow()
method, when invoked on a writer object, takes a list as its input argument and appends it to the csv file referred by the writer object. We will pass the list as the input argument to thewriterow()
method.
After execution of the writerow()
method, the list will be appended to the CSV file. To save the data, you should close the file using the close()
method. Otherwise, no changes will be saved to the csv file.
The source code to append a list to a csv file using the csv.writer() method is as follows.
import csv
myFile = open('Demo.csv', 'r+')
print("The content of the csv file before appending is:")
print(myFile.read())
myList = [4, 'Joel','Golang']
print("The list is:")
print(myList)
writer = csv.writer(myFile)
writer.writerow(myList)
myFile.close()
myFile = open('Demo.csv', 'r')
print("The content of the csv file after appending is:")
print(myFile.read())
Output:
The content of the csv file before appending is:
Roll,Name,Language
1,Aditya,Python
2,Sam, Java
3, Chris, C++
The list is:
[4, 'Joel', 'Golang']
The content of the csv file after appending is:
Roll,Name,Language
1,Aditya,Python
2,Sam, Java
3, Chris, C++
4,Joel,Golang
Conclusion
In this article, we have discussed an approach to append a list to csv file in python. In this approach, the list will be appended to the csv file irrespective of whether it has the same number of elements as compared to the columns in the csv or not. Thus it is advised to make sure that each list should have the same number of elements as compared to the columns in the csv file. Also, You should make sure that the order of element present in the lists should be in accordance with the the columns present in the csv file. Otherwise, the data appended to the csv file will become inconsistent and will lead to errors.
To know more about lists in python, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.