往CSV中写文件 def write_csv_list(list): list_new=[] print(list) print("*"*50) for obj in list: list_new.append([obj[“sku”],obj[“name”],obj[“price”],obj[“brand”]]) print(list_new) print("*" * 50) with open(“data.csv”,‘w’,encoding=“utf8”,newline="")as csvfile: writer =csv.writer(csvfile) writer.writerow([“sku”,“名字”,“价格”,“品牌”]) writer.writerows(list_new) 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”: “vivo r9”, “price”: 3999, “brand”: “vivo” }] write_csv_list(productList) def write_csv_dic(list): with open(“data1.csv”, ‘w’, encoding=“utf8”, newline="")as csvfile: writer=csv.DictWriter(csvfile,[“sku”,“name”,“price”,“brand”]) writer.writeheader() writer.writerows(list) 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”: “vivo r9”, “price”: 3999, “brand”: “vivo” }] write_csv_dic(productList)