×

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 Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

NSE Tools In Python

About NSE NSE (National Stock Exchange) of India Limited is the advanced stock exchange of India. It is located in Mumbai, Maharastra and It was organized in 1992. It was...

2 minutes read.

Difference Between Python and Scala

What is Python? Python is a high-level general-purpose interpreted programing language. It is used for multi general-purpose work such as language construct as well as its object-oriented approach aims to help...

4 minutes read.

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Rank Based Percentile GUI Calculator using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities...

3 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

How to Import Files in 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.

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.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Standard GUI Unit Converter using PyQt5 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...

6 minutes read.

Write Dictionary to CSV 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.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.