Write list to CSV Python
In this post, you will learn how to write a list to CSV using the Python programming language.
A list is an arranged collection of elements. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements in a list. In the development process, we may come to a situation in which we need to write a list to a CSV file.
After dealing with a dataset and performing all pre-processing, we must save the pre-processed information in some data format, such as csv, excel, or others. The CSV (Comma-separated-values) is widely used for data exchange in data-based applications. In web applications, we generally store the data in an array, dataframe, lists, tuples, dictionaries, etc. There may be a need for clients to export this data to a CSV file. The data in CSV is stored as sequences of records. Like in other programming languages, we can also write data to a CSV file.
There are several ways to convert a list to a CSV file in Python. Here, we have mentioned most of them-
Write list to CSV using the csv.writer() Method
For using the csv.writer() method, first we need to import the csv module. Suppose we take the following entries to write into a CSV file.
Id,Name,Language
101,Priska,Python
102,Smith,PHP
103,Kashyap,Java
The given code writes the list to a CSV file-
import csv
with open('developer.csv', 'w', newline='') as developer_file:
writer = csv.writer(developer_file)
writer.writerow(["Id", "Name", "Language"])
writer.writerow([101, "Priska", "Python"])
writer.writerow([102, "Smith", "PHP"])
writer.writerow([103, "Kashyap", "Java"])
The writer() function in the CSV module returns a writer object that converts data into a delimited string and stores in a file object and the writer.writerow() command inserts the entries row-wise into the CSV file. When we run the above codes, the developer.csv file will be created, row-wise, in the current directory with the given entries.
Write list to CSV using the Pandas Method
Python Pandas is open source, fast, easy-to-use tool for data manipulation and data analysis. It provides rich data structures and functions to make data analysis easy and fast. Here, you will learn how to convert the list to CSV using Python pandas. If you haven't installed the pandas module, you can easily install the Pandas module with PIP tool.
pip install pandas
The following code demonstrates how to convert a list to CSV using Python pandas.
import pandas as pd
name = ["Kavya", "John", "Smith", "Somya"]
subject = ["CHM", "PHY", "BIO", "MATHS"]
score = [98, 85, 91, 98]
dict = {'name': name, 'subject': subject, 'score': score}
df = pd.DataFrame(dict)
df.to_csv('subject.csv')
Write list to CSV using the NumPy Method
NumPy is the fundamental package for scientific computing with Python. It is a highly optimised library for numerical operations. The support of NumPy makes the task easier. The following example demonstrates how to use the NumPy library to save a list to a CSV file in Python.
import numpy as np
students_rows = [ ['Smith', 'ENG', 99],
['Krish', 'COM', 89],
['Priska', 'EVS', 81],
['Kavya', 'SCI', 91]]
np.savetxt("numpy_test.csv", students_rows, delimiter =",",fmt ='% s')
Related Articles
Python Convert XML to CSV
Python Pandas Dataframe to CSV
Python Converting a CSV File to a MySQL Table
Convert Excel to CSV Python Pandas
Generate and download a CSV file in Django
Python write to csv file
Python Pandas Dataframe to CSV
Python Pandas CSV to Dataframe
Prettytable in Python
Python dict inside list
Convert dictionary to dataframe Python
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module