Python Forum
importing CSV file into a HTML table using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing CSV file into a HTML table using Python
#1
I am trying to create a program which takes the values inside a CSV file and outputs them as a HTML table format. This is what i got so far. The Headers work fine it all appears as it should. But when i try to read the file it does not import the values inside it.

Here is the code, ill be happy if someone could explain why this happens
#!C:/Users/Tony/python.exe
print("content-type: text/html\n")
import csv
import html

#Create a table
print("<table>")

print("<th>")
print("<td>Student ID</td>")
print("<td>Lab+Class Activity -- Oct9 [Total Pts: 10 Score] |2336087</td>")
print("<td>OCt 10: Class Activities [Total Pts: 10 Score] |2336877</td>")
print("<td>Lab: Oct11 [Total Pts: 10 Score] |2337058</td>")
print("<td>Oct15:Class Activities [Total Pts: 10 Score] |2338422</td>")
print("<td>Oct16 [Total Pts: 10 Score] |2338579</td>")
print("<td>Class Activity Oct17 [Total Pts: 10 Score] |2339309</td>")
print("<td>Lab-Oct18 [Total Pts: 10 Score] |2339494</td>")
print("<td>Oct22, Class Activity [Total Pts: 10 Score] |2340310</td>")
print("<td>Lab-Oct23 [Total Pts: 10 Score] |2340400</td>")
print("<td>Class Activity: OCT24 [Total Pts: 10 Score] |2340833</td>")
print("<td>Lab-Oct25 [Total Pts: 10 Score] |2340970</td>")
print("<td>Final Project [Total Pts: 100 Score] |2340979</td>")
print("<td>Final Project Documentation [Total Pts: 10 Score] |2343801</td>")
print("</th>")

csv1 = open('c:/Users/Tony/File1.csv','r')

for line in csv1:
    row = line.split(",")
    a = float(row[0])
    b = float(row [1])
    c = float(row[2])
    d = float(row[3])
    e = float(row[4])
    f = float(row[5])
    g = float(row[6])
    h = float(row [7])
    i = float(row[8])
    j = float(row[9])
    k = float(row[10])
    l = float(row[11])
    m = float(row[12])
  
print("</table>")
Reply
#2
Probably, you forgot something like this:

print("<tr>")
print("\n".join("<td>%s</td>" % v for v in row))
print("</tr>")
instead of lines 30-42.

However, I would strongly recommend to look (and use it) at some template engine (e.g. jinja2), if you want
to generate html/xml/text etc. formatted documents.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Going through HTML table with selenium emont 3 730 Sep-30-2023, 02:13 AM
Last Post: emont
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 877 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 698 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  python standard way of importing library mg24 1 872 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Use module docx to get text from a file with a table Pedroski55 8 5,791 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  Problem with importing Python file in Visual Studio Code DXav 7 4,898 Jun-15-2022, 12:54 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,329 May-17-2022, 07:49 AM
Last Post: Pedroski55
  HTML file crashes program mikefirth 12 3,701 Dec-31-2021, 03:57 AM
Last Post: Pedroski55
  Importing a function from another file runs the old lines also dedesssse 6 2,478 Jul-06-2021, 07:04 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020