Python Write Multiple Rows in CSV Example

Published On: 03/01/2023 | Category: Python


Hi Guys,

In this quick example, let's see python csv files multiple rows example. you'll learn python write multiple rows to csv file. Here you will learn write multiple lines to csv file python. you can understand a concept of how to write multiple rows in csv using python. Let's get started with python list to csv rows.

In this illustration, we'll make a demo.csv file with fields for ID, Name, and Email. In order to write numerous rows to a csv file, we will establish a "data" list with values. To build a CSV file from a list, we will utilise the open(), writer(), writerow(), writerows(), and close() functions.

Without further ado, let's look at the code example below:

So let's see bellow example:

Example 1:

main.py
import csv
  
# open the file in the write mode
f = open('demo.csv', 'w')
  
# create the csv writer
writer = csv.writer(f)
  
header = ['ID', 'Name', 'Email']

data = [

    [1, 'Bhavesh Sonagra', '[email protected]'],
    [2, 'Nikhil Patel', '[email protected]'],
    [3, 'Piyush Kamani', '[email protected]']
]
  
# write the header
writer.writerow(header)
  
# write a row to the csv file
writer.writerows(data)
  
# close the file
f.close()
Output

It will help you....