Solve the UnicodeEncodeError: 'ascii 'codec can't encode exception of Python2.x.

Source: Internet
Author: User

When I first encountered this problem, I searched for it online for half a day and found that the problem could be solved by adding the following three lines of code,

[Python]
Import sys
Reload (sys)
Sys. setdefaultencoding ('utf-8 ')

Import sys
Reload (sys)
Sys. setdefaultencoding ('utf-8 ')


However, after I added it, I still couldn't solve it. After some thinking and mining, I finally found the underlying cause,

I encountered a problem when I tried to download a novel. When I wrote all the crawled novel content into a file, I reported an error. Because some of the content is not supported by ascii encoding, an error is returned.

So, the better way is, in the output, the file to develop a specific UTF-8 code. You do not need to change the default encoding.


[Python]
# Use codecs instead of open to open a file:
Fp = codecs.open('output.txt ', 'a +', 'utf-8 ′);;
Fp. write (row [1]);
Fp. close ();

# Use codecs instead of open to open a file:
Fp = codecs.open('output.txt ', 'a +', 'utf-8 ′);;
Fp. write (row [1]);
Fp. close ();

The following is a reposted article about codecs.

------------------------------------------------------------ Split line -------------------------------------------------------------


Character encoding is a way to convert single-byte characters and multi-byte characters according to certain rules. From single-byte to multi-byte is called decoding, and from multi-byte to single-byte is called encoding. In these Rules, UTF-8 and GB2312 are commonly used.
 
In Python, The codecs module provides methods to implement these rules. Through the public method of the module, we can easily obtain the Encoder and Decoder Factory functions of a certain encoding method ), and StreamReader, StreamWriter, and StreamReaderWriter.
 
Use "import codecs" to import the codecs module.
 
One of the important functions in the codecs module is lookup. It has only one parameter, namely the encoding method name, UTF-8 or gb2312. Example:
>>> Import codecs
>>> T = codecs. lookup ("UTF-8 ")
>>> Print t
(<Built-in function utf_8_encode>, <function decode at 0x00AA25B0>, <class encodings. utf_8.StreamReader at 0x00AA0720>, <class encodings. utf_8.StreamWriter at 0x00AA06F0>)
>>> Encoder = t [0]
>>> Decoder = t [1]
>>> StreamReader = t [2]
>>> StreamWriter = t [3]
The lookup function returns a TUPLE containing four elements, where t [0] is an encoder function reference and t [1] is a decoder function reference, t [2] is a StreamReader Class Object reference for UTF-8 encoding, t [3] is the StreamWriter Class Object reference for UTF-8 encoding. I believe you are familiar with Python and you will surely know how to use them next.
 
The codecs module also provides separate functions for programmers to simplify lookup calls. They are:
Getencoder (encoding)
Getdecoder (encoding)
Getreader (encoding)
Getwriter (encoding)
If we only want to obtain an encoder method encoded with UTF-8, we only need to do this:
>>> Encoder = codecs. getencoder ("UTF-8 ")
 
In addition, the codecs module provides an open method to simplify StreamReader and StreamWriter. Compared with the open method of the built-in object File, the former has three more parameters: encoding, errors, and buffering. These three parameters are optional, but for applications, you must specify the value of encoding, while errors and buffering can use the default value. The usage is as follows:
>>> Fin = codecs. open ("e: \ mycomputer.txt", "r", "UTF-8 ")
>>> Print fin. readline ()
This is my computer
>>> Fin. close ()
 
To sum up, the codecs module provides the lookup method for processing character encoding. It accepts a parameter of the character encoding name, the function objects and class objects of encoder, decoder, StreamReader, and StreamWriter corresponding to the specified character encoding are also returned. To simplify the call to the lookup method, codecs also provides the getencoder (encoding), getdecoder (encoding), getreader (encoding), and getwriter (encoding) methods. Further, the access to StreamReader, StreamWriter, and StreamReaderWriter of specific character encoding is simplified. codecs provides the open method more directly and passes the character encoding name through the encoding parameter, you can obtain two-way services for encoder and decoder.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.