ItSolutionStuff.com

Python Write CSV File from List Example

By Hardik Savani • October 30, 2023
Python

Hi Dev,

Now, let us see an article of python create csv file from list. I am going to show you about python write csv file from list. This example will help you python make csv file from list. It is a simple example of how to create csv file from list in python. Follow the below tutorial step of how to write csv file from list in python.

In this example, we will create demo.csv file with ID, Name and Email fields. we will create a "data" list with values. we will use open(), writer(), writerow(), writerows() and close() functions to create csv file from list.

So, Without any further ado, let's see below code example:

You can use these examples with python3 (Python 3) version.

Example:

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, 'Hardik Savani', 'hardik@gmail.com'],
	[2, 'Vimal Kashiyani', 'vimal@gmail.com'],
	[3, 'Harshad Pathak', 'harshad@gmail.com'],
]
  
# write the header
writer.writerow(header)
  
# write a row to the csv file
writer.writerows(data)
  
# close the file
f.close()

Output:

You can see csv file layout:

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check If a List is Empty or Not in Python?

Read Now →

How to Read Text File Line by Line in Python?

Read Now →

How to Read a Text File in Python?

Read Now →

How to Append Text or Lines to a Text File in Python?

Read Now →

Python Generate Text File from List Example

Read Now →

How to Reverse List Elements in Python?

Read Now →

Python String Convert to Uppercase Example

Read Now →

How to Remove Duplicate Values from List in Python?

Read Now →

Python Post Request with Basic Authentication Example

Read Now →

How to Check if Today is Saturday or not in Python?

Read Now →

Python PUT Request with Parameters Example

Read Now →

Python POST Request with Parameters Example

Read Now →