Getting Started with Python: input gets \xe4\xb8\xad character correct decoding __python

Source: Internet
Author: User
There are several points to understand to resolve this problem:Unicode encoding is used by default in Python3, which means that support for Chinese will be better, input Chinese, and display Chinese directly. But when assigning "\xe4\xb8\xad" directly to a variable in a py file, Python3 will find the corresponding character directly in Unicode and then display ĸ

When using input to get to "\xe4\xb8\xad", its Essence in memory is "\\xe4\\xb8\\xad", so the show to us is "\xe4\xb8\xad" using the 16 number to indicate Utf-8 will appear "\x" (this is the rule), So we need to convert the 16-in-\x 16-digit number into a 10-digit number, and then make a bytes synthesis. Second, to understand how bytes is used:
#bytes用法

in[80]: a = [1,2,3]
in[81]: bytes (a)
out[80]: B ' \x01\x02\x03 '        #执行结果
#在py文件中直接赋值 ' \xe4\xb8\xad ' Corner decoding method

s = ' \xe4\xb8\xad '
ss = S.encode (' raw_unicode_escape ')
print (ss)           # Result: B ' \xe4\xb8\xad '
sss = Sss.decode ()
print (SSS)          #结果:
Business
#python3.5
#Author: FORYOUSLG

s = ' \xe4\xb8\xad '
ss = input (' Please enter \\xe4\\xb8\\xad ': ')
print ( "S= ' \\xe4\\xb8\\xad")
print ("Ss=input () gets the same character as: ' \\\\xe4\\\\xb8\\\\xad '")
print (' s not equal to SS ')
Print ('
the correct solution is this:
strtobytes = [] for i in                 
ss.split (' \\x '):
    If I!= ':
        num = int (i,16)
        Strtobytes.append (num)

a = bytes (strtobytes). Decode ()
print (a)
"")

strtobytes = [] for         
I In Ss.split (' \\x '):
    If I!= ':
        num = int (i,16)
        strtobytes.append (num)

a = bytes (strtobytes). Decode ()
print (' input () gets the positive result of the "\\xe4\\xb8\\xad" conversion:%s '% a)
Execution Results
please Enter "\xe4\xb8\xad": \xe4\xb8\xad s= ' \xe4\xb8\xad ' Ss=input () the character obtained is such: ' \\xe4\\ Xb8\\xad ' s is not equal to SS the correct solution is such: Strtobytes = [] for i in Ss.split (' \x '): If I!= ': num = int (i Strtobytes.append (num) a = bytes (strtobytes). Decode () print (a) input () Gets the positive result of the "\xe4\xb8\xad" transformation: 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.