The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Convert CSV to HTML tables

Converting CSV data to HTML tables involves parsing the CSV content and formatting it into proper HTML <table>, <tr>, <th>, and <td> tags.

Here’s a clear example of how to convert a simple CSV text into an HTML table:


Example CSV:

pgsql
Name,Age,City Alice,30,New York Bob,25,Los Angeles Charlie,35,Chicago

Converted HTML table:

html
<table border="1" cellspacing="0" cellpadding="5"> <thead> <tr> <th>Name</th> <th>Age</th> <th>City</th> </tr> </thead> <tbody> <tr> <td>Alice</td> <td>30</td> <td>New York</td> </tr> <tr> <td>Bob</td> <td>25</td> <td>Los Angeles</td> </tr> <tr> <td>Charlie</td> <td>35</td> <td>Chicago</td> </tr> </tbody> </table>

If you want a programmatic approach (like in Python), here’s a simple script:

python
import csv csv_data = """Name,Age,City Alice,30,New York Bob,25,Los Angeles Charlie,35,Chicago""" lines = csv_data.splitlines() reader = csv.reader(lines) html = '<table border="1" cellspacing="0" cellpadding="5">n' html += ' <thead>n <tr>n' headers = next(reader) for header in headers: html += f' <th>{header}</th>n' html += ' </tr>n </thead>n <tbody>n' for row in reader: html += ' <tr>n' for cell in row: html += f' <td>{cell}</td>n' html += ' </tr>n' html += ' </tbody>n</table>' print(html)

Let me know if you want the conversion for a specific CSV input!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About