安暖如初

博客园 首页 新随笔 联系 订阅 管理

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)

posted on 2021-06-04 22:57  安暖如初  阅读(35)  评论(0编辑  收藏  举报