Open In App
Related Articles

Convert CSV to HTML Table in Python

Improve Article
Save Article
Like Article

CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma.
Example : 

Name, Salary, Age, No.of years employed
Akriti, 90000, 20, 1
Shreya, 100000, 21, 2
Priyanka, 25000, 45, 7
Neha, 46000, 25, 4

Note: For more information, refer to Working with csv files in Python

Converting CSV to HTML Table in Python

Method 1 Using pandas: One of the easiest way to convert CSV file to HTML table is using pandas. Type the below code in the command prompt to install pandas.

pip install pandas 

Example: Suppose the CSV file looks like this – 




# Python program to convert
# CSV to HTML Table
import pandas as pd
# to read csv file named "samplee"
a = pd.read_csv("read_file.csv")
# to save as html file
# named as "Table"
# assign it to a
# variable (string)
html_file = a.to_html()



Method 2 Using PrettyTable: PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. Type the below command to install this module.

pip install PrettyTable

Example: The above CSV file is used.


from prettytable import PrettyTable
# open csv file
a = open("read_file.csv", 'r')
# read the csv file
a = a.readlines()
# Separating the Headers
l1 = a[0]
l1 = l1.split(',')
# headers for table
t = PrettyTable([l1[0], l1[1]])
# Adding the data
for i in range(1, len(a)) :
code = t.get_html_string()
html_file = open('Tablee.html', 'w')
html_file = html_file.write(code)

Output : 



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Jun, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials