#๐Ÿ”’ Bug in this generating html table python code

65 messages ยท Page 1 of 1 (latest)

unreal depotBOT
#

@minor musk

Python help channel opened

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.

minor musk
#

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)
hexed sandal
#
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

unreal depotBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

hexed sandal
minor musk
hexed sandal
#

In text

minor musk
#

[('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+')]

hexed sandal
#

!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

unreal depotBOT
#

@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>
hexed sandal
#

That looks valid

hexed sandal
#

!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)
unreal depotBOT
#

@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

hexed sandal
#

@minor musk is it possible that the last 3rd line is causing the problem? Like the last line in my screenshot shows empty strings

minor musk
#

I don't know I fetched the results list from my postgres sql database

hexed sandal
minor musk
#

No

hexed sandal
minor musk
#

The script is not giving any errors

hexed sandal
#

ok good

hexed sandal
minor musk
hexed sandal
minor musk
#

Ok, I think the problem is occurring while writing to the .html file

#

The html content outputted to the stdout is fine

minor musk
hexed sandal
#

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)
hexed sandal
#

Because I don't think you understand how replace() works

#

!e

print("Hello".replace("e", "i"))
unreal depotBOT
#

@hexed sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.

Hillo
hexed sandal
#

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)
unreal depotBOT
#

@hexed sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.

<html><table><td>Hi</td></table></html>
hexed sandal
#

ok that does work

minor musk
hexed sandal
#

Because it looks like it should be working end to end

#

Otherwise, can you share the result of templates.html?

#

and the test2.html

minor musk
#

I already shared it above

hexed sandal
#

If its too long, share it in a pastebin

#

!paste

unreal depotBOT
#
Pasting large amounts of code

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.

hexed sandal
#

Use that link and follow the instructions โ˜๏ธ

minor musk
hexed sandal
#

really strange. I ran the code on my machine and Im not able to reproduce the "bug"

minor musk
#

Even when I changed at4 it is still not writing correctly to the file the angular bracket is still missing

hexed sandal
#

I know

#

Its missing in a lot of places but its starting at At4 table header

#

And then it hits every td after that

unreal depotBOT
#
Python help channel closed

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.