python爬虫的各种存储方式之csv
2.csv的存储和读取
判断目录,有则打开,没有新建
import csv
import os
#判断目录,有则打开,没有新建
if os.path.exists('D:\Python\代码\数据爬取'):
os.chdir('D:\Python\代码\数据爬取')
else:
os.mkdir('D:\Python\代码\数据爬取')
os.chdir('D:\Python\代码\数据爬取')
文件写入,需要写入列表; newline:消除空格
with open('mycsv.csv','w',encoding="utf-8",newline='') as csvfile:
write1 = csv.writer(csvfile)
write1.writerow(['编号','网址','昵称'