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])
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐