#๐ Bug in this generating html table python code
65 messages ยท Page 1 of 1 (latest)
@minor musk
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I am trying to create html table using python. But i am getting this weird problem
the code seems correct but when executed the closing bracket of </td> is not written into file "</td"
</td<td>3.0</td<td> this is the problem
html_content = "<table>"
html_header = "<tr><th>S.No.</th><th>Subject Code</th><th>Subject name</th><th>Intmax</th><th>Intmarks</th><th>Extmax</th><th>At1</th><th>At2</th><th>At3</th><th>At4</th><th>At5</th><th>Total</th><th>Perc</th><th>Status</th><th>Grade</th></tr>"
for i in range(0,len(results)):
row = results[i]
html_row = "<tr>"
for col in row:
html_row += f"<td>{col}</td>"
html_row += "</tr>"
html_header += html_row
html_content += html_header
html_content += "</table>"
with open("test2.html", "r") as f:
template_content = f.read()
placeholder = "<!-- Table content -->"
template_content = template_content.replace(placeholder, html_content)
with open("templates.html", "w") as f:
f.write(template_content)
html_content = "<table>"
html_header = "<tr><th>S.No.</th><th>Subject Code</th><th>Subject name</th><th>Intmax</th><th>Intmarks</th><th>Extmax</th><th>At1</th><th>At2</th><th>At3</th><th>At4</th><th>At5</th><th>Total</th><th>Perc</th><th>Status</th><th>Grade</th></tr>"
for i in range(0,len(results)):
row = results[i]
html_row = "<tr>"
for col in row:
html_row += f"<td>{col}</td>"
html_row += "</tr>"
html_header += html_row
html_content += html_header
html_content += "</table>"
with open("test2.html", "r") as f:
template_content = f.read()
placeholder = "<!-- Table content -->"
template_content = template_content.replace(placeholder, html_content)
with open("templates.html", "w") as f:
f.write(template_content)
This is how you code format. And you end it with triple backticks
!code format instructions below
can we get an example of what "results" look like?
It is a list of lists
[('CH20ABS103', 'CHEMISTRY', 40, 37, '60', '49', '', '', '', '', 86, 86.0, 3.0, 3.0, 'P', 'A'), ('CH20ABS104', 'CHEMISTRY LAB', 40, 34, '60', '50', '', '', '', '', 84, 84.0, 1.5, 1.5, 'P', 'A'), ('CS20AES101', 'PROBLEM SOLVING USING C', 40, 38, '60', '47', '', '', '', '', 85, 85.0, 3.0, 3.0, 'P', 'A'), ('CS20AES102', 'PROBLEM SOLVING USING C LAB', 40, 39, '60', '56', '', '', '', '', 95, 95.0, 1.5, 1.5, 'P', 'A+'), ('CS20AES103', 'IT WORKSHOP', 40, 38, '60', '55', '', '', '', '', 93, 93.0, 1.5, 1.5, 'P', 'A+'), ('EE20AES101', 'BASIC ELECTRICAL & ELECTRONICS ENGINEERING', 40, 36, '60', '49', '', '', '', '', 85, 85.0, 3.0, 3.0, 'P', 'A'), ('EE20AES102', 'BASIC ELECTRICAL & ELECTRONICS ENGINEERING LAB', 40, 36, '60', '55', '', '', '', '', 91, 91.0, 1.5, 1.5, 'P', 'A+'), ('EG20AMC101', 'SPEECH & ORAL COMMUNICATION(MC)', 40, 39, '--', '--', '--', '--', '--', '--', 39, 97.5, 0.0, 0.0, 'P', 'Y'), ('MA20ABS101', 'LINEAR ALGEBRA AND CALCULUS', 40, 40, '60', '45', '', '', '', '', 85, 85.0, 3.0, 3.0, 'P', 'A'), ('ME20AES101', 'ENGINEERING WORKSHOP', 40, 37, '60', '57', '', '', '', '', 94, 94.0, 1.5, 1.5, 'P', 'A+')]
something's not right with it
!e
results = [('CH20ABS103', 'CHEMISTRY', 40, 37, '60', '49', '', '', '', '', 86, 86.0, 3.0, 3.0, 'P', 'A')]
html_content = "<table>"
html_header = "<tr><th>S.No.</th><th>Subject Code</th><th>Subject name</th><th>Intmax</th><th>Intmarks</th><th>Extmax</th><th>At1</th><th>At2</th><th>At3</th><th>At4</th><th>At5</th><th>Total</th><th>Perc</th><th>Status</th><th>Grade</th></tr>"
for i in range(0,len(results)):
row = results[i]
html_row = "<tr>"
for col in row:
html_row += f"<td>{col}</td>"
html_row += "</tr>"
html_header += html_row
html_content += html_header
html_content += "</table>"
print(html_content)
Testing
@hexed sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
<table><tr><th>S.No.</th><th>Subject Code</th><th>Subject name</th><th>Intmax</th><th>Intmarks</th><th>Extmax</th><th>At1</th><th>At2</th><th>At3</th><th>At4</th><th>At5</th><th>Total</th><th>Perc</th><th>Status</th><th>Grade</th></tr><tr><td>CH20ABS103</td><td>CHEMISTRY</td><td>40</td><td>37</td><td>60</td><td>49</td><td></td><td></td><td></td><td></td><td>86</td><td>86.0</td><td>3.0</td><td>3.0</td><td>P</td><td>A</td></tr></table>
That looks valid
There's something wrong with these 2 lines:
('MA20ABS101', 'LINEAR ALGEBRA AND CALCULUS', 40, 40, '60', '45', '', '', '', '', 85, 85.0, 3.0, 3.0, 'P', 'A'), ('ME20AES101', 'ENGINEERING WORKSHOP', 40, 37, '60', '57', '', '', '', '', 94, 94.0, 1.5, 1.5, 'P', 'A+')
They don't seem to be valid
!e
results = [('CH20ABS103', 'CHEMISTRY', 40, 37, '60', '49', '', '', '', '', 86, 86.0, 3.0, 3.0, 'P', 'A'), ('CH20ABS104', 'CHEMISTRY LAB', 40, 34, '60', '50', '', '', '', '', 84, 84.0, 1.5, 1.5, 'P', 'A'), ('CS20AES101', 'PROBLEM SOLVING USING C', 40, 38, '60', '47', '', '', '', '', 85, 85.0, 3.0, 3.0, 'P', 'A'), ('CS20AES102', 'PROBLEM SOLVING USING C LAB', 40, 39, '60', '56', '', '', '', '', 95, 95.0, 1.5, 1.5, 'P', 'A+'), ('CS20AES103', 'IT WORKSHOP', 40, 38, '60', '55', '', '', '', '', 93, 93.0, 1.5, 1.5, 'P', 'A+'), ('EE20AES101', 'BASIC ELECTRICAL & ELECTRONICS ENGINEERING', 40, 36, '60', '49', '', '', '', '', 85, 85.0, 3.0, 3.0, 'P', 'A'), ('EE20AES102', 'BASIC ELECTRICAL & ELECTRONICS ENGINEERING LAB', 40, 36, '60', '55', '', '', '', '', 91, 91.0, 1.5, 1.5, 'P', 'A+'), ('EG20AMC101', 'SPEECH & ORAL COMMUNICATION(MC)', 40, 39, '--', '--', '--', '--', '--', '--', 39, 97.5, 0.0, 0.0, 'P', 'Y'),]
html_content = "<table>"
html_header = "<tr><th>S.No.</th><th>Subject Code</th><th>Subject name</th><th>Intmax</th><th>Intmarks</th><th>Extmax</th><th>At1</th><th>At2</th><th>At3</th><th>At4</th><th>At5</th><th>Total</th><th>Perc</th><th>Status</th><th>Grade</th></tr>"
for i in range(0,len(results)):
row = results[i]
html_row = "<tr>"
for col in row:
html_row += f"<td>{col}</td>"
html_row += "</tr>"
html_header += html_row
html_content += html_header
html_content += "</table>"
print(html_content)
@hexed sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
<table><tr><th>S.No.</th><th>Subject Code</th><th>Subject name</th><th>Intmax</th><th>Intmarks</th><th>Extmax</th><th>At1</th><th>At2</th><th>At3</th><th>At4</th><th>At5</th><th>Total</th><th>Perc</th><th>Status</th><th>Grade</th></tr><tr><td>CH20ABS103</td><td>CHEMISTRY</td><td>40</td><td>37</td><td>60</td><td>49</td><td></td><td></td><td></td><td></td><td>86</td><td>86.0</td><td>3.0</td><td>3.0</td><td>P</td><td>A</td></tr><tr><td>CH20ABS104</td><td>CHEMISTRY LAB</td><td>40</td><td>34</td><td>60</td><td>50</td><td></td><td></td><td></td><td></td><td>84</td><td>84.0</td><td>1.5</td><td>1.5</td><td>P</td><td>A</td></tr><tr><td>CS20AES101</td><td>PROBLEM SOLVING USING C</td><td>40</td><td>38</td><td>60</td><td>47</td><td></td><td></td><td></td><td></td><td>85</td><td>85.0</td><td>3.0</td><td>3.0</td><td>P</td><td>A</td></tr><tr><td>CS20AES102</td><td>PROBLEM SOLVING USING C LAB</td><td>40</td><td>39</td><td>60</td><td>56</td><td></td><td></td><td></td><td></td><td>95</td><td>95.0</td><t
... (truncated - too long)
Full output: https://paste.pythondiscord.com/MRNEMW2OH7XR36CUBGQUTLTDXM
All except the last 2 lines are working. Not sure if its a unicode thing but I don't see a problem with the </td>
@minor musk is it possible that the last 3rd line is causing the problem? Like the last line in my screenshot shows empty strings
I don't know I fetched the results list from my postgres sql database
are you getting any errors from your script now?
No
so its all working?
The script is not giving any errors
ok good
Do you still need help? If not, type !close
Ok sorry the html output written to the file is still missing the closing tag
which one?
can you share the output of html_content for example?
Ok, I think the problem is occurring while writing to the .html file
The html content outputted to the stdout is fine
How did u write it to html file
OK so I think your problem is in these then:
with open("test2.html", "r") as f:
template_content = f.read()
placeholder = "<!-- Table content -->"
template_content = template_content.replace(placeholder, html_content)
with open("templates.html", "w") as f:
f.write(template_content)
Yes, can u find the bug?
Is there "<!-- Table content -->" in template_content?
Because I don't think you understand how replace() works
!e
print("Hello".replace("e", "i"))
@hexed sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
Hillo
hmm
!e
template_content = "<html><!-- Table content --></html>"
placeholder = "<!-- Table content -->"
updated_content = "<table><td>Hi</td></table>"
new_content = template_content.replace(placeholder, updated_content)
print(new_content)
@hexed sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
<html><table><td>Hi</td></table></html>
ok that does work
Yes, the html file contains the place holder, which will then be replaced by replace method the idea was given by chatgpt
maybe don't use test2.html? Use a proper template content html?
Because it looks like it should be working end to end
Otherwise, can you share the result of templates.html?
and the test2.html
I already shared it above
where?
If its too long, share it in a pastebin
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
Use that link and follow the instructions โ๏ธ
Comparing your output and mine, it seems that yours is corrupted starting from the "At4"
really strange. I ran the code on my machine and Im not able to reproduce the "bug"
Even when I changed at4 it is still not writing correctly to the file the angular bracket is still missing
I know
Its missing in a lot of places but its starting at At4 table header
And then it hits every td after that
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.