In the process of writing a Python crawler, sometimes the result is written to a TXT file, but you will encounter Unicodeencodeerror.
Cause of error--
An error occurred while writing the contents of the file to the file.
The reason for the error is that the Python system, using the default encoding type, here ASCII, goes to write the corresponding content to the file. However, due to some of the content, ASCII encoding is not supported, so error.
Solution--
Google, Baidu found a solution has two, summarized and recorded:
1.
Import sysreload (SYS) sys.setdefaultencoding (' Utf-8 ')
2.
A better approach is to create a specific UTF-8 code for the file at the time of the output. Without changing the default encoding. Instead of opening the file with open, use the codecs
Import Codecs
fp = Codecs.open (' output.txt ', ' A + ', ' utf-8 ')http://www.qwangxiao.com/t/wenzhang.html
Fp.write (Row[1])
Fp.close ()
python--Write error Unicodeencodeerror workaround