Exporting variable to CSV file in Python
Last Updated :
22 Feb, 2023
The CSV file or comma-separated values file is used to store and share data across platforms. The columns are separated by commas or other relevant delimiters, giving the data a tabular structure.
Sometimes we come across ready-made CSV files while sometimes we need to create one according to the requirements in our projects. Python can write CSV files using many modules. The csv module in Python’s standard library provides classes and methods to read & write data in CSV files.
To write in our CSV file, we use the following objects and methods of the csv module:
The writer() method
This function returns a writer object that converts data into a delimited string and stores it in a file object. The function needs a file object as a parameter. To prevent additional space between lines, the newline parameter is set to ‘’.
The writer class has the following methods:
- csv.writerow()- This function writes items in an iterable (list, tuple, or string), separating them by delimiter
- csv.writerows()- This function takes a list of iterables as a parameter, and writes each of them into new rows.
To write into a CSV file, let us start by creating a variable (List, Tuple, String). We would then export this variable into our file with the help of the above two csv module methods.
Example 1: Exporting a list variable into csv file.
Python3
import csv
# exporting a list variable into the csv file
input_variable = ['This', 'is', 'Geeks',
'For', 'Geeks','list']
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
Output:

Example 2: Exporting a Tuple variable into csv file
Python3
import csv
# exporting a tuple variable into the csv file
input_variable = ('This', 'is', 'Geeks',
'For', 'Geeks', 'Tuple')
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
Output:

Example 3: Exporting a string variable into CSV file
Python3
import csv
# exporting a string variable into the csv file
input_variable = "GeeksForGeeks"
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
Output:

Similarly, we can write multiple rows into a CSV with the help of a 2-D list. This structure of data closely resembles a tabular Excel sheet with rows and columns.
For example:
The below code creates an input variable with a nested list of lists and tuples.
Python3
import csv
# 2D list of variables (tabular data with rows and columns)
input_variable = [
['S.no','name','e-mail'],
[1,'meesha','[email protected]'],
(2,'abhilasha','[email protected]'),
(3,'arav','[email protected]')
]
# Example.csv gets created in the current working directory
with open ('Example.csv','w',newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerows(input_variable)
Output:

Note: Changes in the CSV file will be visible after the successful execution of the above code. You need to review your CSV file after each run.
Similar Reads
How To Print A Variable's Name In Python In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp
3 min read
Get Variable Name As String In Python In Python, getting the name of a variable as a string is not as straightforward as it may seem, as Python itself does not provide a built-in function for this purpose. However, several clever techniques and workarounds can be used to achieve this. In this article, we will explore some simple methods
3 min read
Visualize data from CSV file in Python CSV stands for Comma-Separated Values, which means that the data in a CSV file is separated by commas, making it easy to store tabular data. The file extension for CSV files is .csv, and these files are commonly used with spreadsheet applications like Google Sheets and Microsoft Excel. A CSV file co
4 min read
How to fix 'Variable not defined' in custom modules in Python Encountering the 'Variable not defined' error in Python can be frustrating, especially when working with custom modules. This error indicates that Python cannot find a variable that you are trying to use. In this article, we will explore what this error means, its common causes, and the steps to res
5 min read
Write a dictionary to a file in Python A dictionary store data using key-value pairs. Our task is that we need to save a dictionary to a file so that we can use it later, even after the program is closed. However, a dictionary cannot be directly written to a file. It must first be changed into a format that a file can store and read late
3 min read
How to delete a CSV file in Python? In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap
2 min read
Working with csv files in Python Python is one of the important fields for data scientists and many programmers to handle a variety of data. CSV (Comma-Separated Values) is one of the prevalent and accessible file formats for storing and exchanging tabular data. In article explains What is CSV. Working with CSV files in Python, Rea
10 min read
Print Single and Multiple variable in Python In Python, printing single and multiple variables refers to displaying the values stored in one or more variables using the print() function.Let's look at ways how we can print variables in Python:Printing a Single Variable in PythonThe simplest form of output is displaying the value of a single var
2 min read
Access environment variable values in Python An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va
3 min read