CSV Write in Python
What is meant by CSV?
CSV stands for Comma Separated Values.
The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in spreadsheets or databases.
Spreadsheets are nothing but Excel sheets. The Excel sheets' data are categorized using the concept of Comma Separated Values, i.e., CSV.
CSV converts the data into tables so that the data can be organized and retrieval becomes easier.
Import and export of data become easier when the CSV concept is implemented. The data can be manipulated efficiently, and the reading and writing of the data can be hidden from the programmer.
As we've already discussed, CSV completely works on converting the data into tables.
Every line of the tabled data is known as Record. The files or data that are present in the records are separated by "commas" by default. As commas act as a default character for every record, Comma is said to be known as a Delimiter.
What if we don’t have CSV?
The world without CSV is just like a room without shelves. What if we have no shelves in a room? Can we be able to arrange all the things in an organized way? The answer is no. In the same way, CSV is required compulsorily in order to have organized data.
What is a CSV File?
A plain text file that is used to arrange tabular data by using a specific structure is known as CSV File.
Generally, CSV performs two operations. They are listed below.
- Reading CSV file.
- Writing CSV file.
Reading or Writing CSV files can be done by using "CSV module" and "Pandas".
Now, let's discuss the Writing CSV file.
Writing CSV File using CSV module:
CSV module is already a part of Standard Libraries. So, we need not install the CSV module separately as we already have it in the Standard Libraries by default. You can import CSV to perform "csv write". We can declare a set, i.e., data set to create a CSV file and perform the Write operation. The data set should contain the rows in the form of a list again sub-listed by a parent list.
Let’s consider an example program:
import csv
birthday = [“Name” , “Day “ , “Month “ , ” Year “]
var1 = [
[“Neo”, 30 , ”November” , 2002],
[“Sanneh” , 02 , ”May”, 2002],
[“Samuel” , 10 , ”November” , 2002],
[“David”, 11, ”April” , 2003],
[“Tara” , 31 , ”March” , 2002],
[“Daniel” , 21 , ”May” , 2003],
[“Joe” , 07 , ”March” , 2002],
]
filepath = “ C:\\birthdayblast.csv”
with open(filepath ,’w’) as remind:
var2 = csv.writer(remind)
var2.writerow(birthday)
var2.writerows(var1)
The output of the written program:
Name, Day, Month, Year
Neo, 30, November 2002
Sanneh, 02, May 2002
Samuel, 10, November 2002
David, 11, April 2003
Tara, 31, March 2002
Daniel, 21, May 2003
Joe, 07, March 2002
An explanation for the program:
CSV module is imported initially as it is already present in the standard libraries. Then, a variable "birthday" is declared to list the column names of the data set. Variable "var1" is declared to list out the data entries. Then, a variable "file path" is declared to mention the file's path. The inbuilt functions are called by the variable "var2" for the other variables. Now comes the syntax of CSV write. In the syntax, 'w' indicates write CSV.
Writing CSV File using Pandas:
CSV files can also be imported with the help of pandas.It also performs Data manipulation and provides easy ways to create and delete data. Pandas is not an inbuilt package. So, it must be installed.
Let’s consider an example program:
from pandas import pa
var1 = { ‘ Movie_name ‘ : [ ‘ Bahubali ‘ , ‘ RRR ‘ , ‘ Radhe Shyam ’] ,
‘ Directed_by ‘ : [ ‘ Rajamouli ‘ , ‘ Rajamouli ‘ , ‘ Radha Krishna ‘],
‘ Release_year ‘ : [ ‘ 2017 ‘ , ‘ 2022 ‘ , ’ 2022’ ],
}
var2 = pa( var1, columns = [“Movie_name” , “Directed_by” , “Release_year”])
filepath = “ C:\\movies.csv”
export_csv = var2.to_csv(r’filepath’, index= None, header = True)
print(var2)
The output of the written program:
Movie_name Directed_by Release_year
0 Bahubali Rajamouli 2017
1 RRR Rajamouli 2022
2 Radhe Shyam 2022
An explanation for the program:
After pandas are installed, pa is imported from pandas. A variable "var1" is declared to list the data set, whereas "var2 "lists out the columns. A variable "filepath" is set to store the file's path. The function ".to_csv()" is used to take the written data as a table. Var2 calls this function. So, all the values or data have been stored in var2. Finally, print(var2) is performed to print the data.