Comma Separated Values (CSV) files a type of a plain text document in which tabular information is structured using a particular format. A CSV file is a bounded text format which uses a comma to separate values. The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class.
Example 1:
Creating a CSV file and writing data row-wise into it using writer class.
# Importing library import csv # data to be written row-wise in csv fil data = [['Geeks'], [4], ['geeks !']] # opening the csv file in 'w+' mode file = open('g4g.csv', 'w+', newline ='') # writing the data into the file with file: write = csv.writer(file) write.writerows(data) |
Output:

Example 2:
Writing data row-wise into an existing CSV file using DictWriter class.
# importing library import csv # opening the csv file in 'w' mode file = open('g4g.csv', 'w', newline ='') with file: # identifying header header = ['Organization', 'Established', 'CEO'] writer = csv.DictWriter(file, fieldnames = header) # writing data row-wise into the csv file writer.writeheader() writer.writerow({'Organization' : 'Google', 'Established': '1998', 'CEO': 'Sundar Pichai'}) writer.writerow({'Organization' : 'Microsoft', 'Established': '1975', 'CEO': 'Satya Nadella'}) writer.writerow({'Organization' : 'Nokia', 'Established': '1865', 'CEO': 'Rajeev Suri'}) |
Output:

Example 3:
Appending data row-wise into an existing CSV file using writer class.
# Importing library import csv # data to be written row-wise in csv fil data = [['Geeks for Geeks', '2008', 'Sandeep Jain'], ['HackerRank', '2009', 'Vivek Ravisankar']] # opening the csv file in 'a+' mode file = open('g4g.csv', 'a+', newline ='') # writing the data into the file with file: write = csv.writer(file) write.writerows(data) |
Output:

Recommended Posts:
- Python program to read CSV without CSV module
- Writing CSV files in Python
- Reading and Writing CSV Files in Python
- Load CSV data into List and Dictionary using Python
- Python - Save List to CSV
- Using csv module to read the data in Pandas
- Working with csv files in Python
- Python | Read csv using pandas.read_csv()
- Convert CSV to Excel using Pandas in Python
- Reading CSV files in Python
- Convert JSON to CSV in Python
- Saving Text, JSON, and CSV to a File in Python
- Convert CSV to HTML Table in Python
- Convert HTML table into CSV file in python
- Create a GUI to convert CSV file into excel file using Python
- Convert CSV to JSON using Python
- How to save a Python Dictionary to a CSV File?
- Convert Excel to CSV in Python
- How to Convert an image to NumPy array and saveit to CSV file using Python?
- Convert Text File to CSV using Python Pandas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

