The Wayback Machine - https://web.archive.org/web/20240623130215/https://www.geeksforgeeks.org/writing-data-from-a-python-list-to-csv-row-wise/
Open In App

Writing data from a Python List to CSV row-wise

Last Updated : 04 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Python3




# Importing library
import csv
 
# data to be written row-wise in csv file
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. 

Python3




# 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. 

Python3




# Importing library
import csv
 
# data to be written row-wise in csv file
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:



Similar Reads

Python Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20 Approach: Following are the steps: Sort each r
4 min read
Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
Find the number of negative numbers in a column-wise / row-wise sorted matrix M[][]. Suppose M has n rows and m columns. Example: Input: M = [-3, -2, -1, 1] [-2, 2, 3, 4] [4, 5, 7, 8] Output : 4 We have 4 negative numbers in this matrix We strongly recommend you to minimize your browser and try this yourself first. Naive Solution: Here's a naive, n
15+ min read
Python program to read CSV without CSV module
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the nam
3 min read
Reading and Writing CSV Files in Python
CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. It is one of the most common methods for exchanging data between applications and popular data format used in Data Science. It is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separa
4 min read
Writing CSV files in Python
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the nam
8 min read
Python - Concatenate two list of lists Row-wise
Given two matrices, the task is to write a Python program to add elements to each row from initial matrix. Input : test_list1 = [[4, 3, 5,], [1, 2, 3], [3, 7, 4]], test_list2 = [[1, 3], [9, 3, 5, 7], [8]] Output : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]] Explanation : Matrix is row wise merged. Input : test_list1 = [[4, 3, 5,], [1, 2,
5 min read
How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
How to delete only one row in CSV with Python?
Prerequisites: pandas One can open and edit CSV files in Python via Pandas library. While editing the file one might want to remove the entire row in the file. Following are some different approaches to do the same: Data set in use: iris.csv dataset Method 1: Using slicing This method is only good for removing the first or the last row from the dat
3 min read
Calculate Average for Each Row in a CSV file using Python
Working with CSV files is a common task in data analysis and manipulation. Finding the average of each row in a CSV file becomes crucial when dealing with datasets. In this guide, we'll explore how to achieve this using Python, providing a step-by-step approach and practical examples. dataset.csv total_bill,tip,sex,smoker,day,time,size16.99,1.01,Fe
5 min read
Python - Row-wise element Addition in Tuple Matrix
Sometimes, while working with Python tuples, we can have a problem in which we need to perform Row-wise custom elements addition in Tuple matrix. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [[('Gfg', 3)], [('best', 1)]] cus_eles = [1, 2] Output : [[('
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg