0

I am trying to write a HTML file using python, and I want to print in .html a nested list.

I have written this, but I don´t have any idea about how to doit good.

words = [['Hi'], ['From'], ['Python']]

with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<h1>---------------------------</h1>')

    for i in range(len(words)):
        myFile.write('<tr><td>'(words[i])'</td></tr>')


    myFile.write('</body>')
    myFile.write('</html>')

In .html I want to print the nested list in a table in this similar format:

<body>
    <table>
        <tr>
            <td>Hi</td>
        </tr>
        <tr>
            <td>From</td>
        </tr>
        <tr>
            <td>Python</td>
        </tr>
    </table>
</body>
4
  • What happens when you run your code? What do you want it to do differently? Commented Feb 16, 2021 at 2:37
  • Do you want to be able to handle something like words = [['Hi', 'From', 'Python'], ['Goodbye', 'Java']]? If so, what should the output be? Commented Feb 16, 2021 at 2:40
  • 1
    you are very close, you just need to add <table> and </table>. And myFile.write(print("\n".join(["<tr><td>{}</td></tr>".format(x[0]) for x in words]))) Commented Feb 16, 2021 at 2:41
  • 1
    @D.Seah A nested for loop might be preferrable if the OP wants to be able to handle something like my previous example. Commented Feb 16, 2021 at 2:43

3 Answers 3

2
words = [['Hi'], ['From'], ['Python']]

with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<h1>---------------------------</h1>')

    
    # 2-depth string data to 1-depth 
    words = [word_str for inner in words for word_str in inner] 
    
    # use fstring to build string
    <table>
    for word in words:
        myFile.write(f'<tr><td>{word}</td></tr>') 
    </table>


    myFile.write('</body>')
    myFile.write('</html>')

I tried to edit the accepted answer but I were not available but, you just have to add <table> and </table>

Sign up to request clarification or add additional context in comments.

Comments

1

How about this?

words = [['Hi'], ['From'], ['Python']]

with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<h1>---------------------------</h1>')

    
    # 2-depth string data to 1-depth 
    words = [word_str for inner in words for word_str in inner] 
    
    # use fstring to build string
    for word in words:
        myFile.write(f'<tr><td>{word}</td></tr>')  


    myFile.write('</body>')
    myFile.write('</html>')

1 Comment

Please describe how this solves the OP's problem.
0

You can use tabulate for this

words = [['Hi'], ['From'], ['Python']]
print(tabulate(words, tablefmt="html"))

output:

<table>
<tbody>
<tr><td>Hi    </td></tr>
<tr><td>From  </td></tr>
<tr><td>Python</td></tr>
</tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.