从 python 生成具有列表中两列的 excel 文件

我有两个都有句子的变量,然后我想将它们保存到一个新的 excel 文件中,其中每个句子都在不同的列中。我得到一个“列表”对象没有属性“项目”。


这是我写的:


Use = The dog was sleeping.

Strength = The cat was awake.

volume = [Use, Strength]

!pip install xlsxwriter


import xlsxwriter


workbook = xlsxwriter.Workbook('Jaspersoft.xlsx')

worksheet = workbook.add_worksheet()

bold = workbook.add_format({'bold': True})


worksheet.write('A1', 'Dog', bold)

worksheet.write('B1', 'Cat', bold)


row = 1

col = 0


for case, top in volume.items:

    worksheet.write(row, col, case)

    worksheet.write(row, col + 1, top)

    row +=1


workbook.close()

想要一个有两列标题为 Dog 和 Cat 的 excel 文件,然后在 dog 列下面会有 dog 句子,而 cat 列应该有 cat 句子。


大话西游666
浏览 301回答 1
1回答

喵喔喔

这应该有效:Use = "The dog was sleeping."Strength = "The cat was awake."volume = [Use, Strength]!pip install xlsxwriterimport xlsxwriterworkbook = xlsxwriter.Workbook('Jaspersoft.xlsx')worksheet = workbook.add_worksheet()bold = workbook.add_format({'bold': True})worksheet.write('A1', 'Dog', bold)worksheet.write('B1', 'Cat', bold)row = 1col = 0for i, sentence in enumerate(volume):    worksheet.write(row, col + i, sentence)row +=1workbook.close()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python