python将数据保存为csv格式
python保存csv保存字典数据import csvitem = {'title': '穆斯林的葬礼', 'asin': '', 'url': '', 'brand': ''}fieldnames = ['title', 'asin', 'url', 'brand']with open('数据.csv', mode='a', newline='', encoding='utf-8-sig') a
·
python保存csv
保存字典数据
import csv
item = {'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 = ['https://www.xx.com', 'https://www.yy.com']
with open(search_goods + '.csv', mode='a', newline='', encoding='utf-8-sig') as f:
csv_writer = csv.writer(f, delimiter=',')
csv_writer.writerow([urls])
更多推荐
已为社区贡献1条内容
所有评论(0)