python - 如何使用python xlsxwriter将列表列表添加到excel中
问题描述
我有一个清单l1=[['a','20,'30],['b','30','40']
。我想l1
以这种格式插入到 Excel 文件中:
a 20 30
b 30 40
解决方案
与worksheet.write_column()
_xlsxwriter
>>> import xlsxwriter
>>> a = [['a','20','30'],['b','30','40']]
>>> cl1 = [i[0] for i in a] # ['a', 'b']
>>> cl2 = [','.join(i[1:]) for i in a] # ['20,30', '30,40']
>>> wbook = xlsxwriter.Workbook('Test.xlsx')
>>> wsheet = wbook.add_worksheet('Test')
>>> wsheet.write_column(0,0, cl1)
>>> wsheet.write_column(0,1, cl2)
>>> wbook.close()
或者
你可以使用熊猫pandas.DataFrame.to_excel
>>> import pandas as pd
>>> df = pd.DataFrame.from_dict({'Column1':cl1,'Column2':cl2})
>>> df
Column1 Column2
0 a 20,30
1 b 30,40
>>> df.to_excel('a_name.xlsx', header=True, index=False)
推荐阅读
- sql - 我正在尝试在 SQL 中构建一个视图,该视图识别和分组具有连续日期范围的记录,并返回最早和最晚日期
- jquery - 如何使用 jquery 查询 url 参数?
- javascript - 如何使用图像制作响应式 3 行
- amazon-web-services - 如何在 cloudwatch 日志中使用指标过滤器
- r - 在 R 中使用 textmineR 评估 LDA 预测 - 计算困惑度?
- python - 如何创建在不同句子中提取的标记列表的列表?
- angular - 无法将数据绑定到输入字段
- performance - 在 WCF 终结点中使用服务器名称与 IP 地址
- vue.js - 使用 i18n 设置语言,根据 Vuejs 中选择的语言更改国家/地区标志
- spring - MongoDB relashionships+SpringBoot REST API+POST 方法