Priya R Priya R
Updated date Nov 20, 2023
In this blog, we will learn how to effortlessly convert your CSV data to HTML using Python. Explore multiple methods, including Python's built-in libraries and external tools.

Introduction:

In today's digital world, data is everywhere, and often, we need to present it in a human-friendly format for easier understanding. CSV (Comma-Separated Values) files are a common way to store data, and HTML (Hypertext Markup Language) is a popular format for creating web pages. What if you need to display your CSV data on a webpage? In this blog, we will explore how to convert CSV to HTML using Python.

Method 1: Using Python's Built-in csv and HTML Libraries

Let's start with the simplest way to convert CSV to HTML using Python's built-in libraries. Here's a step-by-step guide on how to do it.

import csv

# Read CSV file
with open('data.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
    data = list(csv_reader)

# Convert to HTML
html_output = "<table>"
for row in data:
    html_output += "<tr>"
    for cell in row:
        html_output += f"<td>{cell}</td>"
    html_output += "</tr>"
html_output += "</table>"

# Save HTML to a file
with open('output.html', 'w') as html_file:
    html_file.write(html_output)

Output:

The output of this program is an HTML file (output.html) that contains a table with the data from the CSV file. When you open this HTML file in a web browser, you will see the CSV data formatted as a table.

In this method, we first open the CSV file and read its content using the csv library. We then iterate through the rows and cells of the CSV and construct an HTML table by appending the data within <table>, <tr>, and <td> HTML tags. Finally, we save the generated HTML content to a file.

Method 2: Using the pandas Library

Another powerful and flexible way to convert CSV to HTML in Python is by using the pandas library. pandas makes it easy to manipulate and analyze data, and it provides a convenient method for exporting data to HTML.

import pandas as pd

# Read CSV file
data = pd.read_csv('data.csv')

# Convert to HTML
html_output = data.to_html()

# Save HTML to a file
with open('output.html', 'w') as html_file:
    html_file.write(html_output)

Output:

The output of this method is an HTML file (output.html) containing a table with the data from the CSV file. When you open the HTML file in a web browser, you'll see a table with the CSV data.

In this method, we start by reading the CSV file into a pandas DataFrame. The to_html() method is then used to convert the DataFrame into an HTML table, which includes table headers (column names). Finally, we save the generated HTML content to a file.

Method 3: Using an External Library

If you need more advanced features or customization, you can consider using external libraries like BeautifulSoup and html-table-extractor in combination with the pandas library. These libraries allow you to fine-tune the conversion process and style the HTML table according to your needs.

import pandas as pd
from bs4 import BeautifulSoup
from html_table_extractor.extractor import Extractor

# Read CSV file
data = pd.read_csv('data.csv')

# Convert to HTML
html_output = data.to_html()

# Parse the HTML
soup = BeautifulSoup(html_output, 'html.parser')

# Customize the HTML (example: change table header background color)
table = soup.find('table')
table['style'] = 'background-color: #f2f2f2;'

# Save HTML to a file
with open('output.html', 'w') as html_file:
    html_file.write(str(soup))

Output:

The output of this method is an HTML file (output.html) with the CSV data presented in a customized HTML table. You can open the HTML file in a web browser to see the results.

In this method, we use pandas to convert the CSV data to an HTML table as in Method 2. After converting to HTML, we parse the HTML content using BeautifulSoup. This step allows us to modify the HTML structure or style, which is demonstrated by changing the background color of the table header in the example. Finally, the modified HTML content is saved to a file.

Conclusion:

In this blog, we have discussed different methods to convert CSV data to HTML using Python. Whether you prefer a simple solution or need more control and customization, Python offers multiple approaches to achieve the desired result. By following the step-by-step instructions and sample code provided, you can easily transform your CSV data into HTML and present it beautifully on a webpage.

Comments (0)

There are no comments. Be the first to comment!!!