×

Excel to CSV in Python

Define MS Excel

Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming language. MS Excel helps save the file quickly, and we can quickly add or delete any data. The key features of MS Excel are:

  • MS Excel allows data filtering so that we can change any values in the MS Excel spreadsheet and add our required data.
  • MS Excel provides the headers and footers, which helps in differentiating the data; it also provides the passwords to protect the data from external users.
  • MS Excel also supports sorting the data so that we can sort our data in ascending or descending order.
  • MS Excel also supports formula auditing; it helps build the relationships between the cells and the tables provided in the data.

Define CSV File

The CSV file is a file that is used to write the text which consists of the data in the spreadsheet the data is separated by the commas. Generally, the Comma separated files (CSV) are mainly used in programming languages, we can import the CSV files into the code of the program with the help of the file declaration. We canwrite the CSV file in notepad or any text file. The CSV file can be opened in any text editor file or any text editor program, but we can't open the excel file only in Microsoft excel or in google sheets only. Generally, the CSV file will be saved with an extension of .csv.

Excel to CSV in Python

The excel file can be converted into a comma-separated file with the help library in the python programming languageknown as the pandas library. The pandas is an open source in python, mainly used to analyze the data. Thepandas dataframe is used to organize the data in a labeled form. The data scientistsprimarily use pandas. The dataframecreated in pandas are mutable. That is, we can insertthe columns into the pandas dataframe. The pandas library in python is faster and simple than any other library.

The pandas are built over the Numpy for operating the pythonlibraries; we require the Numpy. The pandas provide a variety of methods or functions tocomplete the process more easily. To read an excel file we use the read_excel( ) function and we can convert the excel file to a CSV file with the help of to_csv( ).

Code:

# importing the libraries in python


import pandas as pd
# reading the comma-separated value file which is present in excel format


File = pd.read_excel(“Data.xlsx”)
#convering the given file into the comma-separated value file 
file.to_csv(“Data.csv”, index= None, header = True)


#reading the CSV file and converting the file into the dataframe object
df = pd.DataFrame(pd.read_csv(“ Data.csv ”))


#Diaplaying the dataframe
df

Output:

Excel to CSV in Python

 Here, we can observe that the table of the dataframe consists of the data about the name of the person and their Id, and the cost of the book brought by that person. Here, the file was first saved in excel format but then we converted the file into CSV text.

Converting Excel into CSV with xlrd and CSV Library

The xlrd library is used to read the excel file and the CSV file is used to read and write the CSV file. The excel sheets generally can't be imported into the program but the CSV file can be imported easily into the program so easily.

Code:

# importing the libraries which required to perform the required operations


import xlrd
import CSV 
import pandas as pd


#Opening the file with the sheet index


Spreadsheet = xlrd.open_data(“Data.xlsx”)


#Creating the writer's object


Column =csv.writer( open(“D.csv”, ‘w’, newline=” “))


#The data is written into the CSV file
for row in range(Spreadsheet.rows):
      #Writing the data row by row
      # Performing the operation
Column.writerow(Spreadsheet.row_elements(row))


#Reading the CSV file and converting the data into the datframe


df = pd.DataFramre( pd.read_csv(“D.csv”))


#diaplay the data
df

Output:

Excel to CSV in Python

Here, we can observe that the table of the dataframeconsists of the data about the name of the person and their Id, and the cost of the book brought by that person. Here, the file was first saved in excel format but then we converted the file into the CSV text. Here first read the data then copied the data into the CSV file and saved the file in the form of CSV.


Related Topics

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Difference between For Loop and While Loop in Python

What is the “For” loop? The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is...

3 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Artificial intelligence mini projects ideas in python

Artificial intelligence "The human race started from the invention of the wheel", and today we are about to advance to the Industrial level 4.0, where machines can work, talk, think and...

6 minutes read.