A CSV (Comma Separated Values) is a simple file format, used to store data in a tabular format. 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 name for this file format.
There are various methods to save lists to CSV which we will see in this article.
Method 1 : Using CSV Module
import csv # field names fields = ['Name', 'Branch', 'Year', 'CGPA'] # data rows of csv file rows = [ ['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2', '9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1']] with open('GFG', 'w') as f: # using csv.writer method from CSV package write = csv.writer(f) write.writerow(fields) write.writerows(rows) |
Output:

Method 2 : Using Pandas
# importing pandas as pd import pandas as pd # list of name, degree, score nme = ["aparna", "pankaj", "sudhir", "Geeku"] deg = ["MBA", "BCA", "M.Tech", "MBA"] scr = [90, 40, 80, 98] # dictionary of lists dict = {'name': nme, 'degree': deg, 'score': scr} df = pd.DataFrame(dict) # saving the dataframe df.to_csv('GFG.csv') |
Output:

Method 3 : Using Numpy
import numpy as np # data rows of csv file rows = [ ['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2', '9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1']] # using the savetxt # from the numpy module np.savetxt("GFG.csv", rows, delimiter =", ", fmt ='% s') |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- How to save a Python Dictionary to a CSV File?
- Python program to read CSV without CSV module
- Writing data from a Python List to CSV row-wise
- Load CSV data into List and Dictionary using Python
- Python | OpenCV program to read and save an Image
- Python PIL | Image.save() method
- Python - How to save canvas in pyqt5?
- Wand save() method in Python
- How to Filter and save the data as new files in Excel with Python Pandas?
- How to create a bar chart and save in pptx using Python?
- numpy.save()
- Overriding the save method - Django Models
- PyQt5 QSpinBox - How to save its geometry
- Save a dictionary to a file
- How to save a NumPy array to a text file?
- How to load and save 3D Numpy array to file using savetxt() and loadtxt() functions?
- Working with csv files in Python
- Python | Read csv using pandas.read_csv()
- Convert CSV to Excel using Pandas in Python
- Reading CSV files in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

