0110 programming - Chinese coding of Python

Posted by deesse on Mon, 02 Dec 2019 00:36:39 +0100

Click here to enter the AI bar directory and view all articles

Some commonly used Chinese code scrambling transformations collected:

print("ord('I'):",ord('I'))
print("chr(25105):",chr(25105))
print('\n')
print("'My short book'.encode('unicode_escape'):",'My short book'.encode('unicode_escape'))
print("'\\u6211\\u7684\\u7b80\\u4e66'.encode().decode('unicode_escape'):",'\\u6211\\u7684\\u7b80\\u4e66'.encode().decode('unicode_escape'))
print("'\\u6211\\u7684\\u7b80\\u4e66'.encode('ascii').decode('unicode_escape'):",'\\u6211\\u7684\\u7b80\\u4e66'.encode('ascii').decode('unicode_escape'))

print('\n')
print("'My short book'.encode('gbk').decode('ISO-8859-1'):",'My short book'.encode('gbk').decode('ISO-8859-1'))
print("'ÎҵļòÊé'.encode('ISO-8859-1'):",'ÎҵļòÊé'.encode('ISO-8859-1'))
print(r"'\xce\xd2\xb5\xc4\xbc\xf2\xca\xe9'.encode('ISO-8859-1').decode('gbk'):",'\xce\xd2\xb5\xc4\xbc\xf2\xca\xe9'.encode('ISO-8859-1').decode('gbk'))
print("'ÎҵļòÊé'.encode('ISO-8859-1').decode('gbk'):",'ÎҵļòÊé'.encode('ISO-8859-1').decode('gbk'))
print('\n')
print("'My short book'.encode('utf-8').decode('utf-16'):",'My short book'.encode('utf-8').decode('utf-16'))
print("'A kind of\ue791 A kind of껧\ue480ꚹ'.encode('utf-16').decode('utf8','ignore'):",'A kind of\ue791 A kind of껧\ue480ꚹ'.encode('utf-16').decode('utf8','ignore'))

Output results:

ord('I'): 25105
chr(25105): I


'My short book'.encode('unicode_escape'): b'\\u6211\\u7684\\u7b80\\u4e66'
'\u6211\u7684\u7b80\u4e66'.encode().decode('unicode_escape'): My short book
'\u6211\u7684\u7b80\u4e66'.encode('ascii').decode('unicode_escape'): My short book


'My short book'.encode('gbk').decode('ISO-8859-1'): ÎҵļòÊé
'ÎҵļòÊé'.encode('ISO-8859-1'): b'\xce\xd2\xb5\xc4\xbc\xf2\xca\xe9'
'\xce\xd2\xb5\xc4\xbc\xf2\xca\xe9'.encode('ISO-8859-1').decode('gbk'): My short book
'ÎҵļòÊé'.encode('ISO-8859-1').decode('gbk'): My short book


'My short book'.encode('utf-8').decode('utf-16'): A kind ofA kind of껧ꚹ
'A kind ofA kind of껧ꚹ'.encode('utf-16').decode('utf8','ignore'): My short book

If ignore is removed, 'scope \ ue791' ('utf-16 '). Decode ('utf8') will throw an exception Unicode decodeerror: 'UTF-8' codec can't decode byte 0xff in position 0: invalid start byte

ord and chr are the opposite of each other, and characters and ascii codes are interchanged
The slash plus four bits of garbled code can be recovered by. Encode(). Decode ('unicode? Escape ')
Slash plus two bits of garbled code can be recovered with. encode('ISO-8859-1').decode('gbk')
The chaotic code similar to pinyin can be recovered with. encode('ISO-8859-1').decode('gbk')
You can use. encode('utf-16').decode('utf8','ignore ') for garbled code similar to ancient Chinese

Click here to enter the directory of artificial intelligence DBD and watch all the articles

A new era of intelligence for everyone

If you find that the article is wrong, please leave a message to correct;
If you find it useful, please like it;
If you find it useful, please reprint it~

END

Topics: ascii codec