1.引入的内容
productList = [{
"sku": 1,
"name": "iphone 12 pro max",
"price": 12999,
"brand": "苹果"
}, {
"sku": 2,
"name": "mate 40",
"price": 7999,
"brand": "华为"
}, {
"sku": 3,
"name": "vivo r9",
"price": 3999,
"brand": "vivo"
}, {
"sku": 4,
"name": "mi 10",
"price": 1999,
"brand": "小米"
}]
2.开始引入
#(1)引入模块
import xlwt
#(2)工作簿
wb = xlwt.Workbook()
#(3)表
sheet = wb.add_sheet("商品列表")
#(4)写数据:一行一行的写
#表头
list = ["sku","name","price","brand"]
for (i,v) in enumerate(list):
sheet.write(0,i,v)
for (i,v) in enumerate(productList): ##行
for (j,k) in enumerate(list):
#第 i+1(头) 行;第j列
#值 v (字典)中,k头就是键v[k]
sheet.write(i+1,j,v[k])
#(5)保存
wb.save("马冬梅1.xls")