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