![de7d4199910f359954845d8cd289de5a.png](https://img-blog.csdnimg.cn/img_convert/de7d4199910f359954845d8cd289de5a.png)
python版本3.7
在做爬虫时需要将爬取的数据存到本地的txt文档中
最初代码如下
f = open("a.txt","w")
for items in txt:
print(items)
f.write(items+"n",encoding = "utf-8")
上述for items in txt的txt是一个爬完清洗过的list列表,需要将列表中的内容循环写入文本中
运行时,报TypeError: write() takes no keyword arguments
我以为是write()函数不能接受两个参数,所以改成
f = open("a.txt","w")
for items in txt:
print(items)
f.write(items+"n")
运行时,报'gbk' codec can't encode character 'U0001f602' in position 17: illegal multibyte sequence错误
参考了一下网上的解决方案'gbk' codec can't encode character解决方法 将代码改成
f = open("a.txt","w",encoding='utf-8')
for items in txt:
print(items)
f.write(items+"n")
问题解决。