'How to show .csv data file in a more visualized .html file?
Below is is my output .csv file. Is there a way to show the output in .html template file using python to show it in more visualized way?
Date,Text
2021-09-03,"['Hello', 'I', 'am', 'doing', 'well', 'now']"
2021-09-04,"['ABC', 'is', 'good']"
2021-09-15,['Done']
Solution 1:[1]
import pandas as pd
a = pd.read_csv("read_file.csv")
a.to_html("Table.htm")
Another solution:
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)) :
t.add_row(a[i].split(','))
code = t.get_html_string()
html_file = open('Tablee.html', 'w')
html_file = html_file.write(code)
Solution 2:[2]
I agree with @Matvey_coder3's answer that using the prettytable module might be a good option. In addition the Python csv module could be used to read the data. Example below:
import csv
from prettytable import PrettyTable
import webbrowser
# Read CSV data.
with open("date_text.csv", 'r', newline='') as file:
reader = csv.reader(file)
columns = next(reader)
rows = [row for row in reader]
# Create HTML table from it.
tbl = PrettyTable(columns)
for row in rows: # Add data to table.
tbl.add_row(row)
with open('table.html', 'w') as html_file:
html_file.write(tbl.get_html_string())
webbrowser.open('table.html', new=1) # View result.
Here's how the table.html file looked when opened in my web browser:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matvey_coder3 |
| Solution 2 | martineau |

