Unicodeencodeerror: ' ASCII ' codec can ' t encode character ... The workaround

Source: Internet
Author: User

Under python2.7, because you want to read the classification name from the database to write to the file, prompt

Traceback (most recent):   " test.py "  in <module>    fp.write ("%d:%s\r\n"%'  ASCII' codec can't encode character u'\uff08' in position 12:ordinal No in range (+)

No fp.write, print with print but normal, how exactly is this coming back?

# !/usr/bin/python # -*-coding:utf-8-*- Import SYS print sys.getdefaultencoding ();

Run the above program tips

Ascii

So, at the head of the program, add

Import sysreload (SYS) sys.setdefaultencoding ('utf-8')

Run again, error message.

To summarize, python2.7 is based on ASCII to handle the character stream, and when the character stream is not within the ASCII range, an exception is thrown (ordinal not in range (128).

Unicodeencodeerror: ' ASCII ' codec can ' t encode character ... The workaround

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.