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

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()

 

上一篇:php – 变音符号和mb_substr的问题


下一篇:在PHP中检测正确的字符编码?