Convert CSV to HTML Table in Python
×


Convert CSV to HTML Table in Python

1395

Converting CSV Files to HTML Tables Using Python

Working with data often involves reading CSV files and displaying them neatly. One useful approach is to convert CSV data into an HTML table format, which can then be easily embedded in web pages. In this article, we'll explore a straightforward method to achieve this conversion using Python.

What is CSV and Why Convert to HTML?

CSV (Comma Separated Values) files are plain text files used widely for storing tabular data. While CSVs are easy to manipulate programmatically, visualizing them directly can be cumbersome. Converting CSV content into an HTML table makes the data presentable and accessible on web interfaces, making it easier to understand and share.

Step-by-Step Guide to Convert CSV to HTML Table

We'll use Python's built-in csv module to read the CSV data and then generate the corresponding HTML table markup.

1. Reading the CSV File

First, we open the CSV file and use the csv.reader to parse its contents.

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    rows = list(reader)

2. Building the HTML Table

Once we have the CSV data in a list, we can construct an HTML string that represents a table. The first row of the CSV typically contains headers, which we'll use as column titles in the table.

html = '<table>'
html += '<thead><tr>'

for header in rows[0]:
    html += f'<th>{header}</th>'

html += '</tr></thead>'
html += '<tbody>'

for row in rows[1:]:
    html += '<tr>'
    for cell in row:
        html += f'<td>{cell}</td>'
    html += '</tr>'

html += '</tbody></table>'

3. Saving the HTML Content

Finally, you can save the generated HTML string into a file or directly insert it into your web page.

with open('table.html', 'w') as f:
    f.write(html)

Complete Example Code

Here’s the entire script combined for your convenience:

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    rows = list(reader)

html = '<table>'
html += '<thead><tr>'

for header in rows[0]:
    html += f'<th>{header}</th>'

html += '</tr></thead>'
html += '<tbody>'

for row in rows[1:]:
    html += '<tr>'
    for cell in row:
        html += f'<td>{cell}</td>'
    html += '</tr>'

html += '</tbody></table>'

with open('table.html', 'w') as f:
    f.write(html)

Summary

Transforming CSV files into HTML tables with Python is a simple yet powerful way to display data on the web. This approach can be customized further with CSS for styling or enhanced with JavaScript for interactivity. Experiment with this method to present your tabular data cleanly and efficiently!


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat