python爬虫-'gbk' codec can't encode character '\xa0' in position 134: illegal multibyte sequence

时间:2021-04-18 20:19:05
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-95-45a7accf2da0> in <module>
      1 fout = open('job_desc.txt', 'wt')
      2 for info in job_desc:
----> 3     fout.write("{},\"{}\"\n".format(info[0],info[1].replace("\n","").replace(" ","")))
      4 fout.close()

UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 134: illegal multibyte sequence

运行代码如下,报错如上

fout = open('job_desc.txt', 'wt')
for info in job_desc:
    fout.write("{},\"{}\"\n".format(info[0],info[1].replace("\n","").replace(" ","")))
fout.close()

解决方法:

fout = open('job_desc.txt', 'wt',encoding='utf-8')
for info in job_desc:
    fout.write("{},\"{}\"\n".format(info[0],info[1].replace("\n","").replace(" ","")))
fout.close()