python2.7 handling Unicode and ASCII string blending issues

Source: Internet
Author: User

python2.7 default encoding mode is ASCII code, the following can be queried:

Import syssys.getdefaultencoding ()

If you are calculating, comparing, and connecting directly between Unicode and ASCII strings, you will get an error:

' Hello '  = u' hello '= = U
__main__: 1:unicodewarning:unicode equal comparison failed to convert both arguments to unicode-interpreting t Hem as being unequal

The reason for this is that Python converts STR to Unicode and, of course, the results are Unicode, while both STR and Unicode are involved.

The right approach is:

Import sysreload (SYS) sys.setdefaultencoding ('utf-8' Hello '  = u' hello '= = U

The result will return true

ref:http://in355hz.iteye.com/blog/1860787

python2.7 handling Unicode and ASCII string blending issues

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.