当编写python 爬虫demo 写入数据到CSV中或 在公司业务数据中对数据进行拆分、清洗后存储CSV导入数据库时,遇到如下问题的解决方案:
当python写入CSV中间空行时的解决方案为:
设置newline=''
```
with open('content.csv','a+' ,newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(html)
当python写入CSV中文乱码的解决方案为:
设置encoding ='utf-8-sig'
```
with open('content.csv','a+' ,encoding='utf-8-sig') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(html)
最好的解决方案是写入CSV时,对newline和encoding都设置下,这样便不会出现中文乱码及写入出现空行情况
with open('content.csv','a+' ,newline='',encoding='utf-8-sig') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(html)
网友评论