1、嵌有字典列表----csv
1.1
def nestedlist2csv(data,file_path):
with open(file_path, 'w+',encoding='utf-8') as f: #写入文件,出现编码问题,可以改变目标文件的编码方式
w = csv.writer(f)
filednames=['内容','评分','时间','商品信息','可靠度','追加评论天数','追加内容']
w = csv.DictWriter(f,fieldnames=filednames)
w.writeheader()
for comment in comment_data:
w.writerow({
'内容':comment['content'],
'评分':comment['point'],
'时间':comment['time'],
'商品信息':comment['sku_info'],
'可靠度':comment['reliabiltiy'],
'追加评论天数':comment['afterdays'],
'追加内容':comment['replycontent']
})
1.2
def nestedlist2csv(data,file_path):
with open(file_path, 'w+',encoding='utf-8',newline='') as f: #写入文件,出现编码问题,可以改变目标文件的编码方式 w = csv.writer(f)
w = csv.writer(f)
fieldnames=comment_data[0].keys() # solve the problem to automatically write the header
w.writerow(fieldnames)
for row in comment_data:
w.writerow(row.values())