Python 如何将列表转换为HTML表?

Python 如何将列表转换为HTML表?,python,html,tkinter,Python,Html,Tkinter,我正在用Python制作一个web抓取,现在我不得不编写一个文件来生成HTML输出 因此,我有一个名为checked_list的列表,其中包含: ['"link of the picture"', 'title event', 'date event'] 我试着把text1和text2都做了,然后.write()。我可以展示这幅画。但是我需要一个for循环来持续读取所有索引,以便显示图片事件、标题事件和日期事件 我尝试使用text1然后text1+=来编写整个HTML,我尝试使用text1和t

我正在用Python制作一个web抓取,现在我不得不编写一个文件来生成HTML输出

因此,我有一个名为checked_list的列表,其中包含:

['"link of the picture"', 'title event', 'date event']
我试着把
text1
text2
都做了,然后
.write()。我可以展示这幅画。但是我需要一个for循环来持续读取所有索引,以便显示图片事件、标题事件和日期事件

我尝试使用text1然后
text1+=
来编写整个HTML,我尝试使用text1和text2然后组合它们,但我找不到一种方法来使用for循环,因为它总是说语法错误

选中列表的示例

checked_list = ['"http://bneart.com/wp-content/uploads/2019/05/3fbf2374-5cb4-4229-a247-78c2582c5105-817x1024.jpg"', 'Lara Merrett: Flip side', '7th - 25th May'] -- > for 1 event

checked_list = ['"http://bneart.com/wp-content/uploads/2019/05/Foliage-and-Structure-black_small-1024x613.jpg"', 'Helen Wyatt: I Walk the Line', '11th May - 13th July', '"http://bneart.com/wp-content/uploads/2019/05/3fbf2374-5cb4-4229-a247-78c2582c5105-817x1024.jpg"', 'Lara Merrett: Flip side', '7th - 25th May'] -- > 2 events
def print_planner():
选中列表=[]
索引=0
html_文件=打开(规划器_文件,“w”)
text1=''
娱乐节目表
'''
text2=“”
'''
'''
html_file.write(text1)
html_file.write(text2)
html_file.close()
你知道我怎样才能在里面组合成圈吗?它要么只使用text1,要么使用
text1
text2


只要有效,就好。如果你能看到这张照片,我就可以做到了。我想打印标题和下面的日期。

我想这正是你想要的。只需在image_exts元组中为您添加更多图像扩展

def print_planner():
    image_exts = ('.png', '.jpg', '.gif') # add valid image extensions for you
    checked_list = ['data1', 'data2', 'hi', 'picture1.png', 'picture2.png', 'picture3.jpg', 'I am python']
    checked_list = map(str, checked_list) # cast everything in str and persist results.

    text2 = ''.join(
        ['<td><img src="{}" /> </td>'.format(src) for src in checked_list for ext in image_exts if src.endswith(ext)]
    )
    text1 = '''
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <table>
            <tr>
                <th>Entertainment List</th>
            </tr>
            <tr>
                {}  
            </tr>
        </table>

        </table>
    </body>
    </html>
    '''.format(text2)
    with open(planner_file, 'w') as f:
        # using with statement file-stream will be closed automatically.
        f.write(text1)
def print_planner():
image_exts=('.png'、'.jpg'、'.gif')#为您添加有效的图像扩展名
checked_list=['data1','data2','hi','picture1.png','picture2.png','picture3.jpg','I am python']
checked_list=map(str,checked_list)#在str中强制转换所有内容并持久化结果。
text2=''.join(
[''.format(src)用于选中的src\u用于图像中的ext的列表\u exts如果src.endswith(ext)]
)
text1=''
娱乐节目表
{}  
''。格式(文本2)
将打开的(规划器_文件“w”)作为f:
#使用with语句文件流将自动关闭。
f、 写(文本1)

text2应该是什么样子?文本中的2应仅为1?或者很多?@gachdavit是的,所以text1是html文件的初始设置,而text2是我应该开始向html输入checked_列表的地方。这就是为什么我把它分为text1和text2。
def print_planner():
    image_exts = ('.png', '.jpg', '.gif') # add valid image extensions for you
    checked_list = ['data1', 'data2', 'hi', 'picture1.png', 'picture2.png', 'picture3.jpg', 'I am python']
    checked_list = map(str, checked_list) # cast everything in str and persist results.

    text2 = ''.join(
        ['<td><img src="{}" /> </td>'.format(src) for src in checked_list for ext in image_exts if src.endswith(ext)]
    )
    text1 = '''
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <table>
            <tr>
                <th>Entertainment List</th>
            </tr>
            <tr>
                {}  
            </tr>
        </table>

        </table>
    </body>
    </html>
    '''.format(text2)
    with open(planner_file, 'w') as f:
        # using with statement file-stream will be closed automatically.
        f.write(text1)