首页 > 编程语言 > 详细

python excel文件操作

时间:2021-06-05 00:03:22      阅读:19      评论:0      收藏:0      [点我收藏+]

productList = [{

        "sku": 1,

        "name": "iphone 12 pro max",

        "price": 12999,

        "brand": "苹果"

    }, {

        "sku": 2,

        "name": "mate 40",

        "price": 7999,

        "brand": "华为"

    }, {

        "1sku": 3,

        "name": "vivo r9",

         "prbcxice": 3999,

        "brand": "vivo"

    }]

import csv

def write_csv_list(list):

    list_new = []

    print(list)

    for obj in list:

        list_new.append([obj["sku"],obj["name"],obj["price"],obj["brand"]])

    print(list_new)

 

    with open("data.csv",‘w‘,encoding=‘utf8‘) as csvFile:

        writer = csv.writer(csvFile)

        writer.writerow(["sku","名字","价格","品牌"])

        writer.writerow(list_new)

 

def write_csv_dic(list):

    with open("data1.csv",‘w‘,encoding=‘utf8‘,newline=‘‘) as csvFile:

        #存储csv,拿到writer对象

        writer = csv.DictWriter(csvFile,["sku",‘name‘,"price","brand"])

        #writer.writeheader()

        writer.writerows(list)

write_csv_dic(productList)

python excel文件操作

原文:https://www.cnblogs.com/lizecheng/p/14851610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!