Devnote

Web Development Blog Company, Services India

MySQL Error 121 Duplicate key on write - Practical Repair Strategy
MySQL Error 121 Duplicate key on write – Practical Repair Strategy
February 13, 2026
PHP cURL error 60: SSL certificate problem - Windows & Linux Fix
PHP cURL error 60: SSL certificate problem – Windows & Linux Fix
February 11, 2026
Fix localhost refused to connect After Installing XAMPP / WAMP
Fix “localhost refused to connect” After Installing XAMPP / WAMP
February 9, 2026
Composer Error Memory exhausted When Installing Packages - Real Fix
Composer Error: Memory exhausted When Installing Packages – Real Fix
February 6, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide
February 4, 2026
Devnote

Type and hit Enter to search

  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Devnote
  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
python convert CSV to HTML Table

Python convert CSV to HTML Table

Image
Devnote team
October 15, 2020
2
1.9K Views
1 Comment

This tutorial is Python convert CSV to HTML Table. I would perform this task using the Pandas library. you would have to do to get the data into table format is. CSV file is a Comma Separated Value file that uses a comma to separate values.

student deta csv

One of the easiest ways to convert CSV files to HTML tables is by using pandas. pandas is a fast, powerful, flexible, and easy to use open-source data analysis and manipulation tool. Type the below code in the command prompt to install pandas.

Using pandas

$ pip install pandas

Example

import pandas as pd

csv_file = pd.read_csv("csv_file.csv")
csv_file.to_html("html_table.html")
html_file = csv_file.to_html()

Output

csv -to -html data

Using for loop

When you find a row you want to print, output it in HTML table row format.

Example

import csv

table = ""
tr = ""

file_name = "csv_data.csv"
with open(file_name) as file:
    table += "<table border=1 cellpadding=1><tr><th>Name</th><th>Salary</th><th>Age</th></tr>"
    csv_reader_object = csv.reader(file)
    next(csv_reader_object)
   for line in csv_reader_object:
        name = line[0]
        salary = line [1]
        age = line[2]

        tr += "<tr>"
        tr += "<td>%s</td>" % name
        tr += "<td>%s</td>" % salary
        tr += "<td>%s</td>" % age
        tr += "</tr>"

end = "</table>"
html = table + tr + end
files = open("output.html", "w+")
files.write(html)
files.close()

Output

csv -to -html data

Vertical HTML Table

import csv

# CSV data
data = [
    ["name", "raj"],
    ["salary", "1000"],
    ["age", "22"]
]

# Start the HTML table
html_table = '<table border="1">\n'

# Add headers for the table (the first row)
html_table += '<tr><th>Attribute</th><th>Value</th></tr>\n'

# Add rows for each entry in the CSV data
for row in data:
    html_table += f'<tr><td>{row[0]}</td><td>{row[1]}</td></tr>\n'

# End the HTML table
html_table += '</table>'

# Print the resulting HTML table
print(html_table)

Output

Attribute Value
name raj
salary 1000
age 22

Categories:

CSVHTMLpython

Other Articles

How to use json.load() in Python
Previous

How to use json.load() in Python

AttributeError: 'str' object has no attribute 'append'
Next

AttributeError: ‘str’ object has no attribute ‘append’

One Comment

  1. Image hugo says:
    January 27, 2024 at 4:53 am

    How can I display the table vertically?

    name, raj
    salary, 1000
    age, 22

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Fix iOS Safari input type="file" Not Opening Camera - Real Solution

Fix iOS Safari input type=”file” Not Opening Camera – Real Solution

February 16, 2026
HTML Marquee tag

How to use HTML Marquee tag

January 23, 2025
CSS List Property With Example

CSS List Property With Example

December 10, 2024
How to Create a Cookie Consent Popup in JavaScript

How to Create a Cookie Consent Popup in JavaScript

November 24, 2023
Devnote

Devnote provides a collection of tutorials about PHP, Laravel, WordPress, Django, MySQL, Bootstrap, Jquery, Ajax, APIs, CRUD operations, etc.

Devnote © , All Rights Reserved.

Page Links

  • Home
  • About us
  • Blog
  • Contact US
  • Site map

Category

  • PHP
  • Laravel
  • wordpress
  • HTML
  • jQuery

Follow Us

Facebook Instagram Twitter Youtube Linkedin Pinterest
Morbi, Gujarat, India - 363641
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Advertisement