Python: Create CSV file from a List Values


Example Code:

import csv

'''

Example to Convert Python List to CSV
Author: Code2care.org
Date: 27 July 2023
Version: v1.0

'''
data_list = [
    ["Date", "T-Shirt", "Jeans", "Dress", "Shirts"],
    ["2023-07-01", 50, 30, 20, 15],
    ["2023-07-02", 40, 25, 15, 20],
    ["2023-07-03", 60, 35, 25, 18],
    ["2023-07-04", 55, 28, 18, 25],
    ["2023-07-05", 70, 40, 30, 22],
    ["2023-07-06", 80, 45, 35, 30],
    ["2023-07-07", 75, 42, 28, 28]
]

file_name = "sales_data_July_23.csv"

with open(file_name, mode='w', newline='') as file:
    writer = csv.writer(file)

    for row in data_list:
        writer.writerow(row)

CSV file Data

Date,T-Shirt,Jeans,Dress,Shirts
2023-07-01,50,30,20,15
2023-07-02,40,25,15,20
2023-07-03,60,35,25,18
2023-07-04,55,28,18,25
2023-07-05,70,40,30,22
2023-07-06,80,45,35,30
2023-07-07,75,42,28,28
Python List to CSV Output in Excel

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Β© Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap