尝试编写excel表

时间:2018-05-02 15:40:18

标签: python excel xlsx

我正在尝试将这些cookie销售写入Excel工作表,但我收到了“无效语法”错误。有关如何正确做到这一点的任何想法?这是我的代码:

import xlsxwriter

workbook = xlsxwriter.Workbook('hello2.xlsx')
worksheet = workbook.add_worksheet()

cookie_sales = {
['Name': 'Sue', 'Feeny', 'Jimmy', 'Seymour'. 'Shawn', 'Corey', 'Anne', 'Terry', 'Derek', 'Sammy', 'Topanga', 'Alan', 'Mark', 'Joe', 'Elliot' ],
['Age':  23, 99, 23, 11, 31, 55, 35, 44, 71, 41, 39, 22, 16, 25, 32 ],
['Boxes_Sold':  5234, 5000, 4566, 3344, 3123, 534, 335, 654, 7773, 990, 11, 46, 742, 222, 9],
['Flavor': 'Chocolate', 'Cookie Dough', 'Cookie Dough', 'Mint', 'Vanilla', 'Chocolate', 'Vanilla', 'Strawberry', 'Strawberry', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate', 'Chocolate' 'Mint' ],
}

row = 0
col = 0

for name, age, boxes_sold, flavor in (cookie_sales):
worksheet.write(row, col,     name)
worksheet.write(row, col + 1, age)
worksheet.write(row, col + 2, boxes_sold)
worksheet.write(row, col + 3, flavor)
row += 1


workbook.close()

2 个答案:

答案 0 :(得分:1)

不确定你是否真的想要这里的字典或列表。 将它全部更改为列表,这应该会产生您正在寻找的结果:

import xlsxwriter

workbook = xlsxwriter.Workbook('hello2.xlsx')
worksheet = workbook.add_worksheet()

cookie_sales = [
['Name', 'Sue', 'Feeny', 'Jimmy', 'Seymour', 'Shawn', 'Corey', 'Anne', 'Terry', 'Derek', 'Sammy', 'Topanga', 'Alan', 'Mark', 'Joe', 'Elliot' ],
['Age', 23, 99, 23, 11, 31, 55, 35, 44, 71, 41, 39, 22, 16, 25, 32],
['Boxes_Sold', 5234, 5000, 4566, 3344, 3123, 534, 335, 654, 7773, 990, 11, 46, 742, 222, 9],
['Flavor', 'Chocolate', 'Cookie Dough', 'Cookie Dough', 'Mint', 'Vanilla', 'Chocolate', 'Vanilla', 'Strawberry', 'Strawberry', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate', 'Chocolate', 'Mint' ]]

row = 0
col = 0

for name in cookie_sales[0]:
    worksheet.write(row, col, name)
    worksheet.write(row, col + 1, cookie_sales[1][row])
    worksheet.write(row, col + 2, cookie_sales[2][row])
    worksheet.write(row, col + 3, cookie_sales[3][row])
    row += 1


workbook.close()

如果你对字典很好奇,要使用字典来做,这也可以使用你当前的结构:

import xlsxwriter

workbook = xlsxwriter.Workbook('hello1.xlsx')
worksheet = workbook.add_worksheet()

cookie_sales = {
'Name' : ['Sue', 'Feeny', 'Jimmy', 'Seymour', 'Shawn', 'Corey', 'Anne', 'Terry', 'Derek', 'Sammy', 'Topanga', 'Alan', 'Mark', 'Joe', 'Elliot' ],
'Age' : [23, 99, 23, 11, 31, 55, 35, 44, 71, 41, 39, 22, 16, 25, 32],
'Boxes_Sold' : [5234, 5000, 4566, 3344, 3123, 534, 335, 654, 7773, 990, 11, 46, 742, 222, 9],
'Flavor' : ['Chocolate', 'Cookie Dough', 'Cookie Dough', 'Mint', 'Vanilla', 'Chocolate', 'Vanilla', 'Strawberry', 'Strawberry', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate', 'Chocolate', 'Mint' ]}

row = 0
col = 0

for key in cookie_sales.keys():
    worksheet.write(0, col, key) # Writes Heading
    row = 1
    for item in cookie_sales[key]: # Writes Heading contents
        worksheet.write(row, col, item)
        row += 1
    col += 1

workbook.close()    

答案 1 :(得分:0)

Dictionary对象需要结构化为 {key:value,key:value}但你拥有的是{key:value,value,value ...}

值可以是列表或其他数据结构,例如 {key:(value1,value2,value3)}

可以使用语法cookie_sales ['name'] [i]

访问

您可能还需要重新构建循环。