当我们爬虫的时候,
可以先把每一条数据存入字典中:
如:
{
'fatalkk':{"name":"fatalkk","age":20,"sex":"男"},
'none':{"name":"none","none":,"sex":"none"},
.......
}
然后我们可以将整个字典直接用下面的代码,将数据一次性导入csv文件啦~
import csv
# 我们需要插入的dict
dict_1 = {"fatalkk": {"name": "fatalkk", "age": 20, "sex": "男"},"none": {"name": "none","age": "none", "sex": "none"}}
file = open('csv1.csv', 'w')
writer = csv.writer(file)
# 写入首行
writer.writerow(['name', 'age', 'sex'])
list1 = []
for i in dict_1.values():
s = tuple(list(i.values()))
list1.append(s)
writer.writerows(list1)
csv文件中: