python UnicodeEncodeError: 'gbk' codec can't encode character ...

When using Python to write the document, or network data stream is written to a local file, and in most cases will encounter: UnicodeEncodeError: 'gbk' codec can not encode character '\ xa0' in position ... the problem . There are many similar documents on how to solve this problem, but nothing more than encode, decode related to the network, which is the real cause of the problem is it? no. In many cases, we use decode and encode, trying all the various encoding, utf8, utf8, gbk, gb2312 and so on, the others are trying all the coding, but still appears compile time: UnicodeEncodeError: 'gbk' codec can not encode character '\ xa0' in position XXX. collapsed.

    Write a python script in the windows below, coding is a serious problem.

    The network data stream written to the file from time to time, we will encounter several code:

    1: # encoding = 'XXX' herein (i.e. the contents of the first file python line) encoding means encodes the python script file itself, does not matter. As long as the same coding XXX and the file itself on the line. Such as notepad ++ "Format" menu where you can set a variety of coding, then the need to ensure that the coding and encoding XXX settings menu is the same as on the line, different things will complain

    2: network coding data streams such as access to web pages, then the encoded data stream is encoded web pages. Required to decode the decoded unicode encoding.

    3: encoding the target file encoding you want to network data stream is written to a new file, so what I need to specify the encoding of the new file. Write file code such as:

f.write(txt)

Txt is then a string, which is decoded by decode string. The key point is coming: encoding the target file is the culprit cause problems within the meaning of the title. If we open a file:

 f = open("out.html","w")

In the windows below, the default encoding new file is gbk, this is the case, python interpreter will be used gbk encoding to resolve our network traffic txt, however txt this time it is decode had unicode encoding, in which case it will lead to resolve not , the problems mentioned above. The solution is to change the encoding target file:

f = open("out.html","w",encoding='utf-8')

solve.

Guess you like

Origin www.cnblogs.com/fuao2000/p/11284557.html