使用Python将两个列表中的元素循环到Excel电子表格中

2021-10-17 14:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把两个列表写进Excel电子表格。你知道吗

它基本上是在两个列表中循环元素。你知道吗

name_list = ["Mary", "Jack", "David"]
project_list = ["Project X", "Project Y", "Project K"]

结果如下:

enter image description here

这就是我所尝试的:

import xlwt

book = xlwt.Workbook(encoding='cp1252', style_compression = 0)
sheet = book.add_sheet('Sheet1', cell_overwrite_ok = True)

name_list = ["Mary", "Jack", "David"]
project_list = ["Project X", "Project Y", "Project K"]

for num, name in enumerate(name_list):
    for num1, project in enumerate(project_list):
        sheet.write(num, 0, name)
        sheet.write(num1, 1, project)

book.save("C:\\output.xls")

很明显这与我的预期相去甚远。你知道吗

enter image description here