python保存csv
保存字典数据
import csvitem = {'title': '穆斯林的葬礼', 'asin': '', 'url': '', 'brand': ''}fieldnames = ['title', 'asin', 'url', 'brand']with open('数据.csv', mode='a', newline='', encoding='utf-8-sig') as f:writer = csv.DictWriter(f, fieldnames=fieldnames)# 判断表格内容是否为空,如果为空就添加表头if not os.path.getsize('数据.csv'):writer.writeheader() # 写入表头writer.writerows([item])
保存列表数据
urls = ['', '']with open(search_goods + '.csv', mode='a', newline='', encoding='utf-8-sig') as f:csv_writer = csv.writer(f, delimiter=',')csv_writer.writerow([urls])