I have a wonderful sheet of tuples (in principle, I can redo it on a sheet of sheets, on a dictionary of sheets and in general on anything. It doesn’t matter):

lst = [(['her1'], [lol1]), (['her2'], [lol2, 123, lol4]), (['her3'], [lol3324, 555])] 

Here it both str and int comes across ...

And there is a variable with html with which I want to make a table:

 html = """<html> <body> <br></br> <FONT size=4><B>Hello!</B></FONT> <br></br> <table border=1> <tr> <td> <B>ROW1</B> </td> <td> <B>ROW2</B> </td> </tr> <tr> <td>ololo</td> <td>tralala</td> </tr> </table> <BR /> <FONT size=4><B> Bye! </B></FONT> <BR> </B> </FONT> <BR /> </body> </html>""" 

I need instead of these ololo and tralala insert her and lol respectively. That is, you have to do something like this:

 html = """<html> <body> <br></br> <FONT size=4><B>Hello!</B></FONT> <br></br> <table border=1> <tr> <td><B>ROW1</B></td> <td><B>ROW2</B></td> </tr> <tr> <td>her1</td> <td>lol1</td> </tr> <tr> <td>her2</td> <td>lol2</td> </tr> <tr> <td>her3</td> <td>lol3, lol324, lol2131</td> </tr> </table> <BR /> <FONT size=4><B> Bye! </B></FONT><BR> </B></FONT><BR /> </body> </html>""" 

Here's how to do this with python? PS In the list, of course, a thousand times more than her and lol, if this is important ...

    2 answers 2

    It would be possible to use the template engine, and if you have a lot of such documents, this will be the most reasonable way out (see jinja2). But if the problem is one, then it can be easier, for example:

     lst = [(['her1'], ['lol1']), (['her2'], ['lol2', 'lol3', 'lol4'])] doc_template = ''' <html> <body> <br></br> <FONT size=4><B>Hello!</B></FONT> <br></br> <table border=1> <tr> <td> <B>ROW1</B> </td> <td> <B>ROW2</B> </td> </tr> {rows} </table> <BR /> <FONT size=4><B> Bye! </B></FONT> <BR> </B> </FONT> <BR /> </body> </html> ''' row_template = ''' <tr> <td>{header}</td> <td>{values}</td> </tr> ''' document = doc_template.format( rows=''.join(row_template.format( header=', '.join(header), values=', '.join(values) ) for header, values in lst) ) print document 
    • uh-uh, sorry, my fault, did not say that there can only be numbers. In general, TypeError: sequence item 0: expected string, int found. If you write str (header) and str (values), then it breaks each character separately with a comma: ( - user210429
    • ', '.join(map(str, values)) possible. - Anatoly Scherbakov
    • Javadxan, big pasiba, I’m there just started to redo the numbers in the sheet in str. And I need such a crutch method to send a letter only, so I didn’t install any modules and template engines, I stopped at your option. Thanks for your time! - user210429

    There is such an option:

     table = [(['her1'], ['lol1']), (['her2'], ['lol2', 'lol3', 'lol4']), (['her3'], ['lol3324', 'lol434423'])] print('<table>') for row in table: print('<tr>') for ceil in row: print('<td>', ', '.join(ceil), '</td>') print('</tr>') print('</table>') 

    In an amicable way, you need to use some template engine for this.

    • Thanks for your time! - user210429