Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in

Source: Internet
Author: User
Tags stdin

When a crawler writes to a file, there is a write error unicodeencodeerror: ' ASCII ' codec can ' t encode characters in ...

The cause of the problem: portions of the write cannot be encoded in ASCII.

Because the default encoding format is ASCII,

Baidu on many of the solutions that are

Import Sys

Reload (SYS)

Sys.setdefaultencoding (' utf-8 ')

That is, modify the default encoding to Utf-8

However, when you then manipulate other commands on the GUI, there will be an abnormal display.

Borrowing bloggers

Chaoshengmingyue's article

The phrase "The general majority executes reload (SYS)" is simply to be able to modify the Python default character set, which is the ability to call Sys.setdefaultencoding (). However, if you execute reload (SYS) in idle, you will cause the next failure to execute any commands.

At first there was nothing to do with the problem, and later on StackOverflow it was seen that someone had said it. This is because idle, as a GUI shell environment, sets specific standard input, standard output, and standard error output during startup initialization, so that both the input and output are in the idle GUI shell and can be viewed in idle in the following way:

>>> import sys  >>> print sys.stdin  <idlelib.PyShell.PseudoInputFile object at 0x00000000027AF2E8> >>> print sys.stdout <idlelib.PyShell.PseudoOutputFile object at 0x00000000027AF320> >>> print sys.stderr <idlelib.PyShell.PseudoOutputFile object at 0x00000000027AF358> >>> 

If Reload (SYS) is executed manually, these three variables of the SYS module will be reset, causing the output to not be displayed in idle. So the solution is very simple, just need to copy these three variables before reload, reload and then restore back to the line:

>>> stdi,stdo,stde=sys.stdin,sys.stdout,sys.stderr  >>> reload(sys)  >>> sys.stdin,sys.stdout,sys.stderr=stdi,stdo,stde >>> print sys.stdout <idlelib.PyShell.PseudoOutputFile object at 0x00000000027AF320> >>> 

A careful person might think that after Reload (SYS), if the standard input, standard output, and standard error output do not work, then the sentence behind Relaod (SYS) SYS.STDIN,SYS.STDOUT,SYS.STDERR=STDI,STDO, How can stde execute it? Therefore, after reload (SYS), before the recovery, the standard input can still work properly, can be checked by the following code:

>>> Reload (SYS) >>> sys.stdout=stdo >>> Print sys. stdin <open file ' <stdin> ', mode ' R ' at 0x0000000001cab030> >>> print S Ys. stdout <idlelib. Pyshell.pseudooutputfile object at 0x00000000027af320> >>> print sys.  stderr <open file ' <stderr> ', Mode ' W ' at 0x0000000001cab150> >>> 
                       

So the problem is solved, but it has to be stressed:

Never use reload (SYS) easily unless you completely erase the results! It is unwise to execute reload (SYS) just to be able to re-set Python's default encoding!

The Python designer intentionally deletes the setdefaultencoding () method inside the Sys module after the Python initialization is done, in order not to let the Python default encoding be changed at run time to avoid some unknown problems. In fact, there are other, more reliable ways to solve the problem of coding. 》》》》》》》》

So what's the best way to solve this problem is to use the codecs module

F=codecs.open (' file.txt ', ' a ', ' utf-8 ')

Then write it again and there's no problem!

Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in

Related Article

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.