合并两个列表以创建一个 Xlsxwriter 行
Combining two lists to make an Xlsxwriter row
给定两个字符串列表(年份和季度),像这样创建一个 excel 行:
"2001 q1", "2001 q2", ..., "2008 q3", "2008 q4"
这是我目前的代码:
import xlsxwriter
from xlsxwriter import workbook
workbook = xlsxwriter.Workbook('template.xlsx')
worksheet_data = workbook.add_worksheet( "data" )
year = ["2001 ", "2002 ", "2003 ", "2004 ", "2005 ", "2006 ", "2007 ", "2008 "]
quarter = ['q1 ', 'q2 ', 'q3 ', 'q4 ']
for x in year:
for y in quarter:
worksheet_data.write_row(
"A1", #start cell
[x + y]
)
workbook.close()
我需要这个为客户定制表格。
有什么想法吗?
假设您想将 str(x+y) 写入“A1”,您可能需要像这样格式化您的请求:
write_row(1, 1, str(x+y))
参考:https://xlsxwriter.readthedocs.io/worksheet.html
风格方面,我还推荐:
- 不要在
year
和 quarter
数组中放置空格,而是在使用这些值时添加它们,例如:combined = str(x + " " + y)
- 循环一个范围而不是手动创建的(连续+数字)年份列表:
range(2001, 2008+1)
希望对您有所帮助:
import xlsxwriter
from xlsxwriter import Workbook
workbook = xlsxwriter.Workbook('template.xlsx')
worksheet_data = workbook.add_worksheet("data")
data = []
year = ["2001 ", "2002 ", "2003 ", "2004 ", "2005 ", "2006 ", "2007 ", "2008 "]
quarter = ['q1 ', 'q2 ', 'q3 ', 'q4 ']
for x in year:
for y in quarter:
data.append(x + y)
worksheet_data.write_row(0, 0, data)
workbook.close()
write_row(0, 0, data) 是零索引,所以这是 A1.
给定两个字符串列表(年份和季度),像这样创建一个 excel 行:
"2001 q1", "2001 q2", ..., "2008 q3", "2008 q4"
这是我目前的代码:
import xlsxwriter
from xlsxwriter import workbook
workbook = xlsxwriter.Workbook('template.xlsx')
worksheet_data = workbook.add_worksheet( "data" )
year = ["2001 ", "2002 ", "2003 ", "2004 ", "2005 ", "2006 ", "2007 ", "2008 "]
quarter = ['q1 ', 'q2 ', 'q3 ', 'q4 ']
for x in year:
for y in quarter:
worksheet_data.write_row(
"A1", #start cell
[x + y]
)
workbook.close()
我需要这个为客户定制表格。
有什么想法吗?
假设您想将 str(x+y) 写入“A1”,您可能需要像这样格式化您的请求:
write_row(1, 1, str(x+y))
参考:https://xlsxwriter.readthedocs.io/worksheet.html
风格方面,我还推荐:
- 不要在
year
和quarter
数组中放置空格,而是在使用这些值时添加它们,例如:combined = str(x + " " + y)
- 循环一个范围而不是手动创建的(连续+数字)年份列表:
range(2001, 2008+1)
希望对您有所帮助:
import xlsxwriter
from xlsxwriter import Workbook
workbook = xlsxwriter.Workbook('template.xlsx')
worksheet_data = workbook.add_worksheet("data")
data = []
year = ["2001 ", "2002 ", "2003 ", "2004 ", "2005 ", "2006 ", "2007 ", "2008 "]
quarter = ['q1 ', 'q2 ', 'q3 ', 'q4 ']
for x in year:
for y in quarter:
data.append(x + y)
worksheet_data.write_row(0, 0, data)
workbook.close()
write_row(0, 0, data) 是零索引,所以这是 A1.