如何使用python将列表列表写入Excel? [英] How to write a list of list into excel using python?

查看:3462
本文介绍了如何使用python将列表列表写入Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python 3将列表列表写入Excel?

How to write a list of list into excel using python 3?

new_list = [["first", "second"], ["third", "fourth"]]
with open("file_name.csv", 'w') as f:
    fc = csv.writer(f, lineterminator='\n')
fc.writerows(new_list)

我可以将此列表写到csv文件中,但是我要如何在excel文件中写呢?

I can write this list into csv file but How canI want to write in excel file?

推荐答案

这是使用 XlsxWriter :

import xlsxwriter

new_list = [['first', 'second'], ['third', 'four'], [1, 2, 3, 4, 5, 6]]

with xlsxwriter.Workbook('test.xlsx') as workbook:
    worksheet = workbook.add_worksheet()

    for row_num, data in enumerate(new_list):
        worksheet.write_row(row_num, 0, data)

输出:

这篇关于如何使用python将列表列表写入Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆