-
作用
将爬取的数据存放到本地的csv文件中
1、导入模块 2、打开csv文件 3、初始化写入对象 4、写入数据(参数为列表) import csv with open('film.csv','w') as f: writer = csv.writer(f) writer.writerow([])
-
示例代码
创建 test.csv 文件,在文件中写入数据
# 单行写入(writerow([])) import csv with open('test.csv','w',newline='') as f: writer = csv.writer(f) writer.writerow(['步惊云','36']) writer.writerow(['超哥哥','25']) # 多行写入(writerows([(),(),()] # newline = '':在Windows中默认会添加1个空行,所以加入newline = ''参数,避免空行出现 import csv with open('test.csv','w',newline='') as f: writer = csv.writer(f) writer.writerows([('聂风','36'),('秦霜','25'),('孔慈','30')])